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

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


Obviously, there's still one issue though: we are using lots of variables and helpers that all belong to the "waypoints" type, but which clutter our source code. So, next we are going to look into an even more flexible approach that nicely maps each waypoint field to a symbolic name, without having to remember vector indices.
Next, we could come up with some helper functions to deal with the vector details transparently:
 
<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 waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
 
var get = func (n,what) {
  return waypoints[n][what];
}
 
var set = func (n,what,value) {
  waypoints[n][what]=value;
}
 
print( get(0, ALTITUDE)  ) # prints 1
print( get(0, DISTANCE)  ) # prints 1000
print( get(1, ALTITUDE)  ) # prints 2
print( get(1, DISTANCE)  ) # prints 1500
 
</syntaxhighlight>
 
 
You could even add some more high level helpers for each waypoint field:
 
<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 waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]]
 
var get = func (n,what) {
  return waypoints[n][what];
}
 
var set = func (n,what,value) {
  waypoints[n][what]=value;
}
 
var get_alt = func(n) {
  return get(n,ALTITUDE);
}
 
var get_dist = func(n) {
  return get(n,DISTANCE);
}
 
var get_angle = func(n) {
  return get(n,ANGLE);
}
 
var get_length = func(n) {
  return get(n,LENGTH);
}
 
var get_id = func(n) {
  return get(n,ID);
}
 
var get_brg = func(n) {
  return get(n,BEARING);
}
</syntaxhighlight>
 
 
Obviously, there's still one issue though: we are using lots of variables and helpers that all belong to the "waypoints" type, but which clutter our source code.  
 
So, next we are going to look into an even more flexible approach that nicely maps each waypoint field and all helper functions to a symbolic name, without having to remember vector indices and such: using Nasal hashes.


== A hash based version (recommended) ==
== A hash based version (recommended) ==

Navigation menu