Community discussions

MikroTik App
bbs2web
Member Candidate
Member Candidate
Topic Author
Posts: 231
加入: Sun Apr 22, 2012 6:25 pm
Location:Johannesburg, South Africa
Contact:

How do I reference array using variable?

Sun Jan 07, 2018 2:13 pm

I need some assistance on how I can reference arrays using a variable:
Code:Select all
:local ifs {"ether1"; "ether2";}; :local ether1 {1.1.0.0/16; 2.2.0.0/16;}; :local ether2 {192.168.1.0/24; 192.168.2.0/24;}; :local num 1; :put ($ether1->$num); :foreach if in=$ifs do={ :put ($if->$num); }

Output:
Code:Select all
2.2.0.0/16

Expected output:
Code:Select all
2.2.0.0/16 2.2.0.0/16 192.168.2.0/24
Top
用户头像
BlackVS
Member Candidate
Member Candidate
Posts: 171
加入: Mon Feb 04, 2013 7:00 pm
Contact:

Re: How do I reference array using variable?

太阳2018年1月07,具体点

A) ROS not supports dynamic names for variable
B) but you can use script from script trick as here -viewtopic.php?f=9&t=125253&p=617239#p617239
Top
bbs2web
Member Candidate
Member Candidate
Topic Author
Posts: 231
加入: Sun Apr 22, 2012 6:25 pm
Location:Johannesburg, South Africa
Contact:

Re: How do I reference array using variable?

Mon Jan 08, 2018 3:09 pm

Thanks, I assumed as much and worked around this limitation by using nested arrays (similar to a hash table). For those that are interested herewith a post with a functional script:
viewtopic.php?f=14&t=129381&p=635752#p635752

Herewith sample array definition:
Code:Select all
:local ifs { {"filter-external"; 1; 1; {}; {}}; {"filter-external-src"; 1; 0; {}; {}}; {"filter-peer1-vlan645"; 1; 1; {205.37.17.206}; {}}; {"filter-peer2-vlan417"; 1; 1; {205.19.28.105}; {}}; {"filter-ixp1"; 1; 1; {205.60.8.156; 10.20.200.0/22}; {}}; {"filter-upstream1-vlan143"; 1; 1; {205.22.33.34}; {}}; {"filter-upstream2-vlan480"; 1; 1; {205.25.155.42}; {}}; {"filter-upstream3-vlan85"; 1; 1; {205.79.22.66}; {}}; {"filter-upstream4-vlan782"; 1; 1; {205.242.227.65}; {}}; };

Then when working with the information in the array:
Code:Select all
:foreach if in=$ifs do={ :local name ($if->0); :local prefixes ($if->3); # load manual prefixes array as '$prefixes' # ie. when name = 'filter-ixp1' then '$prefixes->1' = 10.20.200.0/22 ... }

A) ROS not supports dynamic names for variable
B) but you can use script from script trick as here -viewtopic.php?f=9&t=125253&p=617239#p617239
Top

Who is online

Users browsing this forum:leleand 18 guests