Community discussions

MikroTik App
 
beginners
just joined
Topic Author
Posts: 3
Joined: Fri Aug 23, 2013 10:59 am

assign value to array

Thu Nov 14, 2013 11:25 am

:global currentBuf [ :toarray [ /log find buffer=pppoe ] ] ;
#长度可以自己进行设置(buffer 日志长度)。
:global ontime [:toarray $ontime]
#记录帐号登录时间的数组。
:global currentLineCount [ :len $currentBuf ] ;
:global content;
:global mytime;
:global location;

:for i from=($currentLineCount -1) to=($currentLineCount-10) do={ \
:set content [/log get [ :pick $currentBuf $i ] message;]
:set mytime [:totime [/log get [ :pick $currentBuf $i ] time ] ];
:log warning ($mytime.(" ").$content )
#间接判断字符串中是否存在字符串。
if ([find $content "logged in"]> 0) do={
:set location (:tonum [:pick $content 0 [find $content "logged in"]])
:log warning ($location)
:if ([:pick $ontime $location]="" ) do={:set ontime ($ontime $location $mytime); log warning "aaa" }
:if ([:pick $ontime $location]<$mytime ) do={ /system script run [/system script find name=("chang-route".$location)];

:set ontime [$ontime $location $mytime] ;
:log warning ($location.( route change))}
}
}


in my script,i want to record pppoe account logging time to an array named ontime,i can get the time ,but i don't know how to assign value to array elment, anyone could help me ?
thanks
 
User avatar
skot
Long time Member
Long time Member
Posts: 586
Joined: Wed Nov 30, 2011 3:05 am

Re: assign value to array

Mon Nov 18, 2013 10:24 am

It looks like you are trying to add / edit array items in the array, but there is no way currently to do this, though it sounds like there are plans: http://forum.mikrotik.com/viewtopic.php ... 10#p395914

You can add values to the end of an array, but this probably isn't helpful in your situation:
:set ontime ($ontime , "$location $mytime")
-or-
:set ontime ($ontime , $location)
:set ontime ($ontime , $mytime)
There might be a way to script a workaround somehow, but I can't think of it at the moment. What are some of the $location and $mytime pairs that you would be seeing, or some examples?
I don't need any karma... I have Ιησους Χριστος!
 
sja
newbie
Posts: 29
Joined: Sun Jan 22, 2012 6:26 pm
Location: Italy

Re: assign value to array

Wed Dec 18, 2013 8:36 am

I use the following function in my configuration scripts. Because it rewrites the array you might need to be careful in when you invoke it.
# modifies a single entry in an array.  Natively ROS doesnt have this function
:global setARRAYENTRY do={
# Parameters:
#   $1 the array
#   $2 index of entry to be changed
#   $3 new value of entry to be changed
	:local MYARRAY
	:set MYARRAY
	:for i from=0 to=([:len $1]-1) do={
		:if ($i = $2) do={
			:set MYARRAY ($MYARRAY, $3)
		} else={
			:set MYARRAY ($MYARRAY, [:pick $1 $i])
		}
	}
	:return $MYARRAY
}
This is how I use it:
:global mystore
.....
# set up initial mystore here
......
:local index 42
:local newvalue 1234567
:set mystore [$setARRAYENTRY $mystore  $index $newvalue ]
Hope it helps

Who is online

Users browsing this forum: No registered users and 46 guests