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

Jump to navigation Jump to search
m
m (pedantry: indentation)
Line 173: Line 173:
var waypoint4 = [1,1000,12,22,44,"none", 33];
var waypoint4 = [1,1000,12,22,44,"none", 33];
var waypoint5 = [2,1500,22,42,14,"none", 133];
var waypoint5 = [2,1500,22,42,14,"none", 133];
var waypoints = [waypoint4, waypoint5]
var waypoints = [waypoint4, waypoint5];
</syntaxhighlight>
</syntaxhighlight>


Line 179: Line 179:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var waypoints = [
[1,1000,12,22,44,"none", 33],  
[2,1500,22,42,14,"none", 133]
];
</syntaxhighlight>
</syntaxhighlight>


Accessing such a vector with embedded (or nested) vectors is still simple:
This is just formatted for clarity, the following snippet would be equivalent, but not as readable to people new to vectors:
 
<syntaxhighlight lang="php">
var waypoints = [ [1,1000,12,22,44,"none", 33],  [2,1500,22,42,14,"none", 133] ];
</syntaxhighlight>
 
 
Accessing such a vector with embedded (or nested) vectors is still as simple:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Line 195: Line 205:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
# these are used self-explanatory indices - so that you don't need to remember each field's position:
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
print(waypoints[0][ALTITUDE]) # prints 1
print(waypoints[0][ALTITUDE]) # prints 1
Line 228: Line 240:
</syntaxhighlight>
</syntaxhighlight>


Next, we could come up with some helper functions to deal with the vector details transparently:
Next, we could come up with some helper functions to deal with the vector details transparently, using the same method as before: self-explanatory indices, which spares us having to remember the purpose of each field:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]];


var get = func (n,what) {
var get = func (n,what) {
Line 242: Line 254:
}
}


print( get(0, ALTITUDE)  ) # prints 1
print( get(0, ALTITUDE)  ); # prints 1
print( get(0, DISTANCE)  ) # prints 1000
print( get(0, DISTANCE)  ); # prints 1000
print( get(1, ALTITUDE)  ) # prints 2
print( get(1, ALTITUDE)  ); # prints 2
print( get(1, DISTANCE)  ) # prints 1500
print( get(1, DISTANCE)  ); # prints 1500
</syntaxhighlight>
</syntaxhighlight>


Line 255: Line 267:
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6;
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]];


var get = func (n,what) {
var get = func (n,what) {

Navigation menu