Board index FlightGear Development Nasal

Howto use a var or string as array-pointer  Topic is solved

Nasal is the scripting language of FlightGear.

Howto use a var or string as array-pointer

Postby kuifje09 » Sat Sep 10, 2016 3:31 pm

Silly subject but don't know how to say it else...

I have to create a string or variable which contains the name of some array and its index.
Then I need to use that var as the name for that array, but all I can is print the name of the array and index insead of its content.

This is some part as example:
Code: Select all
#
# show the 5 properties chosen
#
   for ( var i = 0 ; i < 5 ; i+=1 ) {
     a="PrpArr"~Curr~"["~i~"]";
     print (a);
   };


To make it a little more clear... I do buildup the name of the array : PrpArr which should become then PrpArrX[] ... PrpArrY[]
The X and the Y represent the PrpArr which runs from 0 to many... set by the var Curr.
And its index should be made in the loop, from 0 to 4, set by the var i.

( later on this has to be used as a variable property, to be used in <property-prop> )

Then I can put it into var a and print it, and it shows perfect, PrpArr6[0] , but How to get the content its pointing to ?
Say the content is "one" .... "Five", then it should print :
one
two
three
four
five

As variable prop I am done right now, but I would like to check it at the console before....

Understand my problem ? Any help is appreciated. I could not figure it out.
kuifje09
 
Posts: 596
Joined: Tue May 17, 2011 9:51 pm

Re: Howto use a var or string as array-pointer

Postby kuifje09 » Sat Sep 10, 2016 5:50 pm

Did some further testing but it does not do what I want.

Thsi code ( just a little piece ) :
Code: Select all
var PrpArr1 = ["GPS 1","instrumentation/gps/wp/wp[1]/ID",
                "instrumentation/gps/wp/wp[1]/distance-nm","0","0"];
...
#
# show the 5 properties chosen
#
   for ( var i = 0 ; i < 5 ; i+=1 ) {
     a="PrpArr"~Curr~"["~i~"]";
     setprop("/tmp/lcds/ovl[" ~ x ~ "]/p["~i~"]",a);
     print (a);
   };


Gives me this result for the 0 series ( there are 5 ) in the proptree, while I want the content of the array beeing listed there...
p = 'PrpArr0[0]' (string)
p[1] = 'PrpArr0[1]' (string)
p[2] = 'PrpArr0[2]' (string)
p[3] = 'PrpArr0[3]' (string)
p[4] = 'PrpArr0[4]' (string)

But I whant the content of PrpArr0[] .

Noone some idea?
kuifje09
 
Posts: 596
Joined: Tue May 17, 2011 9:51 pm

Re: Howto use a var or string as array-pointer

Postby PINTO » Sat Sep 10, 2016 5:52 pm

So you're trying to assign an array variable based on a string variable?

You can't assign a variable name/array name based on another string variables contents. Why not use a multidimensional array?
Actively developing the MiG-21bis (github repo) (forum thread) (dev discord) (fg wiki)

http://opredflag.com is an active flightgear dogfighting community (using a system that isn’t bombable)
User avatar
PINTO
 
Posts: 966
Joined: Wed Oct 21, 2015 7:28 pm
Callsign: pinto
Version: stable
OS: Win10

Re: Howto use a var or string as array-pointer

Postby PINTO » Sat Sep 10, 2016 5:53 pm

So it'd be
Code: Select all
a = PrpArr[Curr][i]
Actively developing the MiG-21bis (github repo) (forum thread) (dev discord) (fg wiki)

http://opredflag.com is an active flightgear dogfighting community (using a system that isn’t bombable)
User avatar
PINTO
 
Posts: 966
Joined: Wed Oct 21, 2015 7:28 pm
Callsign: pinto
Version: stable
OS: Win10

Re: Howto use a var or string as array-pointer

Postby kuifje09 » Sat Sep 10, 2016 7:01 pm

Thank, I thought I tried that also, but lets try again. Maby it was a typo before...
kuifje09
 
Posts: 596
Joined: Tue May 17, 2011 9:51 pm

Re: Howto use a var or string as array-pointer

Postby Hooray » Sat Sep 10, 2016 7:21 pm

Note that you can do exactly what you want to do by using the compile() API - however, with that being said, I think your problem is ill-defined, you seem to be making up an unnecessarily convoluted solution for a problem that you didn't specify properly.

Thus, my suggestion would be to carefully explain to us what you would like to do, without making any assumptions about the approach/method you are using.
Ideally, in a form that does not add any implicit dependencies (aircraft, FDM, autopilot or scenery).

For example, by posting the problem and a snippet of self-contained code that people can try/execute via the Nasal console.
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: Howto use a var or string as array-pointer  

Postby kuifje09 » Sat Sep 10, 2016 7:53 pm

Thanks all, but the problem is solved.

I started wrong in the first place, filling the array in a wrong way. should be like a = [[a],[b], .... ];
Now the multi-dim array is working. just as expected. I now can use the normal print ( a[x][y] );
kuifje09
 
Posts: 596
Joined: Tue May 17, 2011 9:51 pm


Return to Nasal

Who is online

Users browsing this forum: No registered users and 1 guest