Community discussions

MikroTik App
 
berzerker
newbie
Topic Author
Posts: 44
Joined: Thu Oct 26, 2017 6:55 am

Referencing value in array of variables

Mon Nov 14, 2022 8:55 am

Hello, I have the following code example:
:local $array { "item1"="value1"
		"item2"="value2"
		"item3"="value3"
		}
I would like to reference, say, "value2" specifically as a single value in a specifically created new variable.

So I would end up with "newVariable" specifically outputting only "value2"

I tried $array->1->1 but that doesn't seem to work, can anyone point me in the right direction?

Thank you.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Referencing value in array of variables

Mon Nov 14, 2022 9:57 am

The code example do not work at all,
first, you can not directly declare one variable with $ on name
second, this is not one array, missing all ; between items.

the correct code, regardless the final purpose, is
:local array { "item1"="value1"; "item2"="value2"; "item3"="value3" }

and the correct way to recall value is, for example:
:local newVariable ($array->"item1")
 
berzerker
newbie
Topic Author
Posts: 44
Joined: Thu Oct 26, 2017 6:55 am

Re: Referencing value in array of variables

Mon Nov 14, 2022 5:05 pm

The code example do not work at all,
first, you can not directly declare one variable with $ on name
second, this is not one array, missing all ; between items.

the correct code, regardless the final purpose, is
:local array { "item1"="value1"; "item2"="value2"; "item3"="value3" }

and the correct way to recall value is, for example:
:local newVariable ($array->"item1")
Thanks, sorry, you're right, that's not actually what I have in the script, let me enter it exactly:
:local array { "item1"="value1"; \
	       "item2"="value2"; \
	       "item3"="value3"; 
	     }
However, both of the array elements (item and value) are customizable, so I wanted to find a way where I could reference the "first" element's "value" as both "item" and "value" are unknown to me before anyone changes the items in their own implementation (i.e. position 0,1) hence me trying something like ->0->1

Would there be any way to reference the values if both item and value are unknown? I'd also like to be able to reference "item1" and "value1" separately for different purposes.
Last edited by berzerker on Mon Nov 14, 2022 5:30 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Referencing value in array of variables

Mon Nov 14, 2022 5:19 pm

Working concept:
{
    :local array { "item1"="value1"; "item2"="value2"; "item3"="value3" }
    :foreach x,y in=$array do={
        :put "$x=$y"
    }
}
Last edited by rextended on Mon Nov 14, 2022 5:37 pm, edited 1 time in total.
 
berzerker
newbie
Topic Author
Posts: 44
Joined: Thu Oct 26, 2017 6:55 am

Re: Referencing value in array of variables

Mon Nov 14, 2022 5:31 pm

Exactly??? with 3 { one for each line???

Working concept:
{
    :local array { "item1"="value1"; "item2"="value2"; "item3"="value3" }
    :foreach x,y in=$array do={
        :put "$x=$y"
    }
}
It's morning here, you'll have to forgive me :P edited to update.

Is there any way of doing it without a for loop? Surely there must be some way to reference a single value without iterating over the entire loop
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Referencing value in array of variables

Mon Nov 14, 2022 5:37 pm

No, the index/label must be know for retrieve the variable name

for retrieve the value, for example the first, you can use
:put [:pick $array 0]
but you do not have the variable name, but only the value.

Who is online

Users browsing this forum: loloski and 71 guests