Community discussions

MikroTik App
 
User avatar
fischerdouglas
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 97
Joined: Thu Mar 07, 2019 6:38 pm
Location: Brazil
Contact:

Help with array created by print as-value - Empty value

Thu Mar 27, 2025 7:10 pm

Why is that value coming empty?

The better way to explain my doubt is by showing what is happening:
Mikrotik_Array_Print_as-value.jpg
[user@mikrotik] > /file print                        
 # NAME                                              TYPE             SIZE LAST-MODIFIED       
 0 bla.backup                                        backup       190.8KiB 2025-03-27 13:46:47 
[user@mikrotik] > /file print detail                 
 0 name=bla.backup type=backup size=190.8KiB last-modified=2025-03-27 13:46:47 
[user@mikrotik] > :global test [/file print as-value]
[user@mikrotik] > :put [:typeof $test]               
array
[user@mikrotik] > :put $test                         
.id=*1f01011f;last-modified=2025-03-27 13:46:47;name=bla.backup;size=195393;type=backup
[user@mikrotik] > :put ($test->"name")               

[user@mikrotik] > :put ($test->name)                 

[user@mikrotik] > 
It is an array, correct?
I'm specifying the attribute I want, correct?

Why I'm not being able to print the value of name atribute?
What I'm missing here?
You do not have the required permissions to view the files attached to this post.
 
User avatar
fischerdouglas
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 97
Joined: Thu Mar 07, 2019 6:38 pm
Location: Brazil
Contact:

Re: Help with array created by print as-value - Empty value  [SOLVED]

Thu Mar 27, 2025 7:15 pm

Hummm... I found what was wrong.
Mikrotik_Array_Print_as-value-Array-of-Array.jpg
[user@mikrotik] > :put ($test->"name")               

[user@mikrotik] > :put ($test->name)                 

[user@mikrotik] > :put ($test->1->"name")

[user@mikrotik] > :put ($test->0->"name") 
bla.backup
[user@mikrotik] > 
It is an array of array.
You do not have the required permissions to view the files attached to this post.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4715
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Help with array created by print as-value - Empty value

Thu Mar 27, 2025 10:33 pm

Yeah there are two types of arrays, one is a dictionary/map and other is list. When accessing members, dicts/maps use ->"name", while list use NO quotes ->3.

And "print as-values" uses an array that be described as a "list-of-dictionaries" — why you need to two arrow operators:
[print as-value]->0->"attributes-name"

One tip is use serialize to see what the array looks like, and then using the right ->#->"name" becomes a little easier to see:
:put [:serialize to=json [/ip/arp/print as-value] options=json.pretty]
Basically anything in JSON that uses a [] needs to use -># format. If you see a {} then its a map/dictionary so you need the ->"name" style.


2nd tip be you can get a CSV using from any "print as-value" — there you need the options=dsv.remap which is needed for the $array->#->"name" structure:
:put [:serialize to=dsv  [/ip/arp/print as-value] delimiter=, options=dsv.remap ]
(and you can compare when NOT reproving the dsv.remap)

2.5th tip be you can "pivot" a print as-value by doing both serialize and deserialize:
:put ([:deserialize from=dsv delimiter=, [:serialize to=dsv  [/ip/arp/print as-value] delimiter=, options=dsv.remap ]]->".id") 
which is actually the same as :put [/ip/arp/find]

The important part is this changes the order used to access the array from list-of-dictionary to a dictionary-of-lists:
$array->"name"->0
from the results of "print as-value" — so you could get a simple list of the ANY attribute. So it was "name" field:
([:deserialize from=dsv ... [:serialize to=dsv [.../print as-value] ... ]]->"name")
 
msatter
Forum Guru
Forum Guru
Posts: 2945
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Help with array created by print as-value - Empty value

Thu Mar 27, 2025 10:48 pm

It is called a 2D array and there are more array lines after the first line. This because more than one file exits in /file
> :local test {{name="cccc"; name1="ddddd"};{name="eeeee"; name1="fffff"}}; put $test; put {$test->"name1"}     
name=cccc;name1=ddddd;name=eeeee;name1=fffff

> :local test {{name="cccc"; name1="ddddd"};{name="eeeee"; name1="fffff"}}; put $test; put {$test->0->"name1"} 
name=cccc;name1=ddddd;name=eeeee;name1=fffff
ddddd
 > :local test {{name="cccc"; name1="ddddd"};{name="eeeee"; name1="fffff"}}; put $test; put {$test->1->"name1"} 
name=cccc;name1=ddddd;name=eeeee;name1=fffff
fffff

@ammo there are indeed two different array types but that is not changing the basic working here.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4715
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Help with array created by print as-value - Empty value

Thu Mar 27, 2025 11:16 pm

It is called a 2D array
I had feature request for :serialize to=csv... and in the case MikroTik called the "print as-value" array (which lead to dsv.remap) as a "list-of-dictionaries". Everyone has their own preferred terms it seems. :)

@ammo there are indeed two different array types but that is not changing the basic working here.
I was referring the accessors do vary on based on the array "sub-type".
 
A9691
Frequent Visitor
Frequent Visitor
Posts: 55
Joined: Sat May 14, 2016 10:58 am

Re: Help with array created by print as-value - Empty value

Fri Mar 28, 2025 8:07 am

Maybe this saves some time for someone:
[admin@MikroTik] /ip/address> print
Flags: D - DYNAMIC; S - SLAVE
Columns: ADDRESS, NETWORK, INTERFACE
#    ADDRESS             NETWORK        INTERFACE    
0 DS 192.168.171.117/24  192.168.171.0  ether5       
1    192.168.12.254/24   192.168.12.0   lanbridge    
2    10.12.100.254/24    10.12.100.0    publicbridge 

[admin@MikroTik] /ip/address> :put ([print as-value]->2->"address") 
10.12.100.254/24
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 13000
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Help with array created by print as-value - Empty value

Sat Mar 29, 2025 11:27 am

Not at all, in fact you teach badly and they learn worse.
It is wrong, you should never use numbers or IDs in the script.
I understand that was just an example, but wrong examples are still wrong.

Because, for example, in the terminal there is no point in doing all this to get the IP, then, if it works visually, it is thought that it is the correct way to do the scripts.
:put [get publicbridge address]