Board index FlightGear Development Nasal

Property node and subnodes to vector or hash

Nasal is the scripting language of FlightGear.

Property node and subnodes to vector or hash

Postby Bjoern » Thu Mar 12, 2020 11:05 pm

What's the best way to put a complete property, with all its subnodes and subsubnodes, etc. into a vector or hash?

In my case, I want to map "/input/mice/mouse".

I want my code to accomodate nondefault mouse mappings.


- Edit:

Code: Select all
var testhash = {};
      testhash = props.globals.getNode("/input/mice/mouse").getChildren();
      
      foreach(var key; keys(testhash)){
         print(testhash[key]);
      }


This throws a "bad arguments to keys" error.

Code: Select all
var testhash = {};
      testhash = props.globals.getNode("/input/mice/mouse").getChildren();
      
      forindex(var key; testhash){
         debug.dump(testhash[key]);
      }


This dumps the correct subnodes with key, value and type, but no further type or key analysis works.
Bjoern
 
Posts: 484
Joined: Fri Jan 06, 2012 11:00 pm
Location: TXL (RIP)
Version: Next
OS: ArchLinux

Re: Property node and subnodes to vector or hash

Postby Hooray » Fri Mar 13, 2020 12:16 am

See the Node.getValues() helper, working example here:

http://wiki.flightgear.org/Howto:Proces ... ialog_node
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Property node and subnodes to vector or hash

Postby Bjoern » Fri Mar 13, 2020 11:26 pm

Do you mean this?

Code: Select all
dlgHash = dlgNode.getValues();
var window = canvas.Window.new([200,150],"dialog")
           .set('title',dlgHash.name);


Accessing dlgHash.name requires knowing that there is a key named "name" in the hash. Not really what I want, unless I completely misunderstood the function.
Bjoern
 
Posts: 484
Joined: Fri Jan 06, 2012 11:00 pm
Location: TXL (RIP)
Version: Next
OS: ArchLinux

Re: Property node and subnodes to vector or hash

Postby Hooray » Sat Mar 14, 2020 12:39 pm

var v=Node.getValues():

And then foreach(var k;keys(v))
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Property node and subnodes to vector or hash

Postby Bjoern » Mon Mar 16, 2020 8:35 pm

Hooray wrote in Sat Mar 14, 2020 12:39 pm:var v=Node.getValues():

And then foreach(var k;keys(v))


That makes more sense.

I'm gonna give it a try when I have the time. Thanks so far!
Bjoern
 
Posts: 484
Joined: Fri Jan 06, 2012 11:00 pm
Location: TXL (RIP)
Version: Next
OS: ArchLinux

Re: Property node and subnodes to vector or hash

Postby jsb » Mon Mar 16, 2020 10:06 pm

EDIT: corrected typos mentioned below - thanks for the hint.

To clarify, as it may be confusing in the begining, Nasal knows two different (but somehow similar) composite data types: vector and hash. They are similar in that they store an arbitrary number of elements (which can be either primitive like numbers, booleans, strings or again a vector or hash.
The difference lies in the way the elements are "addressed": a vector (similar to "array" in other languages) automatically numbers the elements from 0 to n-1
Vectors are created using squared bracket and you can only access elements by an integer (index) like this
Code: Select all
var myvector = ["foo", "bar", 42, ];
print(myvector[0]);

A hash on the other hand is not limited to integers as keys, you can also use strings as a key. A hash is defined using { }
Code: Select all
 var myhash = {
    "foo":  "bar",
 foo2: "key without quotes is ok",
   42: "the answer",
};
print(myhash[42]);



Now, you can use
Code: Select all
forindex (var idx; myvector) { print(idx, ": ", myvector[idx]); }
only on vectors while you can use
Code: Select all
foreach (var key; myhash) { print(key, ": ", myhash[key]); }
on hashes and also on vectors - for a vector the keys will be the integer indices.

Only for hashes you can use the keys(myhash) function which returns a vector containing all keys of a hash

Last but not least you can use
Code: Select all
 if (typeof(foo) == "hash" or typeof(foo) =="vector") { do_stuff(); }
if you plan some vector in hash or hash in vector thing.
Last edited by jsb on Wed Mar 18, 2020 9:28 pm, edited 1 time in total.
jsb
 
Posts: 285
Joined: Sat Oct 25, 2014 9:17 pm
Location: Hamburg, Germany
Callsign: D-JSB
Version: next
OS: Win7/Linux

Re: Property node and subnodes to vector or hash

Postby Hooray » Tue Mar 17, 2020 8:55 am

that's pretty good and a much better explanation than what I posted here, I'd suggest to add this to the wiki - so that people can find such information more easily.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Property node and subnodes to vector or hash

Postby Bjoern » Tue Mar 17, 2020 5:50 pm

This confirms what I could gather about vectors and hashes in Nasal. Lua, which I'm more familiar with, differs in expression a bit, but works fundamentally the same.


P.S: Shouldn't this
Code: Select all
forindex (var idx; myvector) { print(i, ": ", myvector[i]); }

be this
Code: Select all
forindex (var idx; myvector) { print(idx, ": ", myvector[idx]); }

and this
Code: Select all
 if (typeof(foo) == "hash" or typeoff(foo) =="vector") { do_stuff(); }

be this?
Code: Select all
 if (typeof(foo) == "hash" or typeof(foo) =="vector") { do_stuff(); }


(Note iterator variable name and typo.)
Bjoern
 
Posts: 484
Joined: Fri Jan 06, 2012 11:00 pm
Location: TXL (RIP)
Version: Next
OS: ArchLinux

Re: Property node and subnodes to vector or hash

Postby Hooray » Tue Mar 17, 2020 6:09 pm

yes, you're correct
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Property node and subnodes to vector or hash

Postby jsb » Wed Mar 18, 2020 9:21 pm

Bjoern wrote in Tue Mar 17, 2020 5:50 pm:This confirms what I could gather about vectors and hashes in Nasal. Lua, which I'm more familiar with, differs in expression a bit, but works fundamentally the same.


P.S: Shouldn't this
Code: Select all
forindex (var idx; myvector) { print(i, ": ", myvector[i]); }

be this
Code: Select all
forindex (var idx; myvector) { print(idx, ": ", myvector[idx]); }

and this
Code: Select all
 if (typeof(foo) == "hash" or typeoff(foo) =="vector") { do_stuff(); }

be this?
Code: Select all
 if (typeof(foo) == "hash" or typeof(foo) =="vector") { do_stuff(); }


(Note iterator variable name and typo.)


Correct, thanks :)
jsb
 
Posts: 285
Joined: Sat Oct 25, 2014 9:17 pm
Location: Hamburg, Germany
Callsign: D-JSB
Version: next
OS: Win7/Linux


Return to Nasal

Who is online

Users browsing this forum: No registered users and 5 guests