Nasal scripting language: Difference between revisions

Jump to navigation Jump to search
m
Line 336: Line 336:
  var z={};      # z is a local hash (dictionary or table) - also used for OOP
  var z={};      # z is a local hash (dictionary or table) - also used for OOP
</syntaxhighlight>
</syntaxhighlight>
Now you may be wondering, what the heck is a vector, and what is a hash - and where's the difference?
The difference is, elements in a vector are sequentially numbered, i.e. each element has a numeric index:
<syntaxhighlight lang="php">
var my_vector = ['A','B','C'];
</syntaxhighlight>
This initializes a vector with three elements: A, B and C.
Now, to access each element of the vector, you would need to use the element's numerical index:
<syntaxhighlight lang="php">
var my_vector = ['A','B','C'];
print (my_vector[0] ); #prints A
print (my_vector[1] ); #prints B
print (my_vector[2] ); #prints C
</syntaxhighlight>
As can be seen, indexing starts at 0.
Compared to vectors, hashes don't use square brackets but curly  braces instead:
<syntaxhighlight lang="php">
var my_hash = {};
</syntaxhighlight>
Now, hashes may not just have numerical indexes, but also symbolic indexes as lookup keys:
<syntaxhighlight lang="php">
var my_hash = {first:'A',second:'B',third:'C'};
</syntaxhighlight>
This will create a hash (imagine it like a storage container for a bunch of related variables) and initialize it with three values (A,B and C) which are assigned to three different lookup keys: first, second, third.
In other words, you can access each element in the hash by using its lookup key:
<syntaxhighlight lang="php">
var my_hash = {first:'A',second:'B',third:'C'};
print ( my_hash.first ); # will print A
print ( my_hash.second ); # will print B
print ( my_hash.third ); # will print C
</syntaxhighlight>


Nasal supports a "nil" value for use as a null pointer equivalent:
Nasal supports a "nil" value for use as a null pointer equivalent:

Navigation menu