Howto:Start using vectors and hashes in Nasal: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
mNo edit summary
Line 72: Line 72:
</syntaxhighlight>
</syntaxhighlight>


waypoints being a vector, we can use a foreach loop to process each element easily:
To do this using a conventional for loop, you would use the size() function to get the size of the vector:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
foreach(var wpt; waypoints)
for(var index; index < size(waypoints); index=index+1) {
print(waypoints[index]);
}
</syntaxhighlight>
 
 
Now, waypoints being a vector, we can also use a simple foreach loop, that works without a counter, to process each element easily:
 
<syntaxhighlight lang="php">
var waypoints = ["wp1","wp2","wp3","wp4","wp5"];
foreach(var wpt; waypoints) {
  print(wpt);
  print(wpt);
}
</syntaxhighlight>
</syntaxhighlight>


Line 94: Line 105:
== A vector based version ==
== A vector based version ==


One simple way to accomplish this is using a another vector for each waypoint, nested inside the original vector. So that we end up with a two-dimensional data structure. For example, imagine a folder containing sub folders, with folders not having names but rather indices.
One simple way to accomplish this is using a another vector for each waypoint, nested inside the original vector. So that we end up with a two-dimensional data structure. For example, imagine a folder containing sub folders, with folders not having names but rather indices (numbers).


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">

Navigation menu