Community discussions

MikroTik App
 
alger
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 80
Joined: Tue Dec 19, 2006 12:35 pm
Location: Russia
Contact:

shift array

Thu Nov 01, 2018 12:51 pm

Hello

:global aaa {a=1;b=2;c=3;d=4;be=5}
:foreach k,v in=$aaa do={:put ("$k=$v")}
result is
a=1
b=2
c=3
d=4
e=5

How to shift value in the aray:
a=2
b=3
c=4
d=5
e=6 (new value)

The task is to store only the last 5 values.
Maybe there are other solutions how to do it?
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 209
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: shift array

Sat Nov 10, 2018 12:48 pm

1st: use local instead of global to prevent issues(local is auto, global is static)
2nd: use "=" inside array index is not needed, use($array->index) better to get or set values
This example may help you to solve your issue
:do {
:local array {1; 2; 3; 4; 5; 6; 7; 8; 9; 10};
 :for i from=0 to=([:len $array]-2) do={
 :set ($array->i) ($array->(i+1));
 }
 :put $array;
} on-error={:log error "ERROR"}

Output will get
2;3;4;5;6;7;8;9;10;10

Who is online

Users browsing this forum: No registered users and 46 guests