Community discussions

just joined
Topic Author
Posts: 17
Joined: Tue Feb 23, 2016 4:50 pm

Add value to the end of an array?

Mon Dec 31, 2018 11:02 pm

So, I'm having an interesting time trying to find how to add a value to the end of an array. I'd like to use an array to ethernet errors detected in a for loop but I'm not finding a way to add to an array easily?

I've got the logic down for finding the count of interfaces, polling the interfaces for each specific type I want then passing that value to a global array errorlist, but the syntax I'm using currently produces an array where the previous entire array is a single variable.

Any ideas?

(and yes, I know the last :log is currently just logging the array variable output to info, will work on setting up an e-mail tool call once I get this array figured out, and that tx-packet are not errors)

local ethstattype [:toarray value="rx-too-short,rx-pause,rx-fcs-error,rx-align-error,rx-drop,rx-jabber,tx-pause,tx-deferred,tx-late-collision,tx-drop,tx-fcs-error,tx-packet"];
foreach iface in=[/interface ethernet find] do={
local ifacename [/interface ethernet get value-name=name number=$iface];
foreach i in=[/interface ethernet find default-name="$ifacename"] do={
foreach ethstat in=$ethstattype do={
local ethstatvalue [:tonum [/interface ethernet get $i value-name=$ethstat]];
if ( $ethstatvalue > -1 ) do={
local alert "$ifacename has $ethstatvalue $ethstat errors";
global errorlist [:toarray value="$alert"];
/interface ethernet reset-counters $ifacename
foreach etherror in=$errorlist do={
:put $etherror;
:log info $etherror;
global thishassomething $etherror;
User avatar
Posts: 392
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany

Re: Add value to the end of an array?

Tue Jan 01, 2019 12:42 pm

This creates empty array and adds a value:
:local array [ :toarray "" ];
:set array ( $array, $newvalue );
Just repeat for more values.
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts

Who is online

Users browsing this forum: No registered users and 7 guests