Community discussions

MikroTik App
 
maximt
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 71
Joined: Tue Jul 20, 2010 6:29 am

is there any associative array, hash, map?

Sat Aug 17, 2013 10:53 am

is there any associative array, hash, map?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: is there any associative array, hash, map?

Sat Aug 17, 2013 4:26 pm

The change log for 6.2 implies that there is in that version with the statement:
*) console - array value syntax in expressions '{1;2;3;4}' now can
specify values with word keys as '{a=1;b=2}';
but I don't really see it working as you'd expect, i.e.

ros code

:pick $assoc "a"
will NOT give you the element with a key called "a", and there isn't a different command that would. What's more puzzling is that

ros code

:pick $assoc 0
will not give you just the value, but also the "a=" prefix... in other words, it's EXACTLY as if associative arrays are not supported at all, and you're getting a plain ol' string.

I guess what the change is ACTUALLY saying is that is no longer considered a syntax error (but is instead interpreted as a plain string), whereas it previously would be.

With all that said - apparently no. There are no associative arrays in scripting (yet?).
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
maximt
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 71
Joined: Tue Jul 20, 2010 6:29 am

Re: is there any associative array, hash, map?

Sun Aug 18, 2013 9:02 am

I wrote two helper functions to store values in environment as global vars.
but there is bug - you can pass variables to functions.
Waiting for 6.3...

ros code

:global setHashValue do={
	:global hashSetter [:parse ":global $1$2 $3"];
	$hashSetter;
}  

:global getHashValue do={
	:global hashLastValue nil;
	:global hashGetter [:parse ":global $1$2 ; :global hashLastValue \$$1$2" ];
	$hashGetter;
	:return $hashLastValue;
} 

$setHashValue "myHash" "keyA" 123;
$setHashValue "myHash" "keyB" 456;
$setHashValue "myHash" "keyC" 678;

:log info [$getHashValue "myHash" "keyA"];
:log info [$getHashValue "myHash" "keyB"];
:log info [$getHashValue "myHash" "keyC"];
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: is there any associative array, hash, map?

Tue Sep 17, 2013 3:00 pm

I just saw this topic, which seems to suggest that there ARE associative arrays, and you can access a key with
$array->"key"
But then, I tried to define one from scripting, but never managed to match a key (an empty string was returned instead). I tried
:local assoc ({},"a"="test1")
:local assoc ({"a"="test1"})
:local assoc {"a"="test1"}
all with the same result.

Side note - that last syntax produces an array with two numeric members - value "a" at offset 0, and "test1" at offset 1.

(I'm using 6.4)

@MikroTik support
Is there anyway an associative array can be defined by a user (and then accessed using the above, or different, syntax), or is this currently just internally supported? If just internally at the moment - any plans to add it for users in the (near) future?
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6111
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: is there any associative array, hash, map?

Tue Sep 17, 2013 3:08 pm

The change log for 6.2 implies that there is in that version with the statement:
*) console - array value syntax in expressions '{1;2;3;4}' now can
specify values with word keys as '{a=1;b=2}';
but I don't really see it working as you'd expect, i.e.

ros code

:pick $assoc "a"
will NOT give you the element with a key called "a", and there isn't a different command that would. What's more puzzling is that

ros code

:pick $assoc 0
will not give you just the value, but also the "a=" prefix... in other words, it's EXACTLY as if associative arrays are not supported at all, and you're getting a plain ol' string.

ros code

[admin@x86] > :global aaa {a=1;b=2;c=3}
[admin@x86] > :put ($aaa->"a")
1
[admin@x86] > :put ($aaa->"b") 
2
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: is there any associative array, hash, map?

Tue Sep 17, 2013 3:12 pm

Well... THAT seems to work. And I also tried time and Boolean values, and they work too.

In that case, it seems the problem is limited just to string values - they are always empty.

And also, the new "->" is not yet documented.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6111
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: is there any associative array, hash, map?

Tue Sep 17, 2013 3:16 pm

Could you post some example with strings that doesn't work?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: is there any associative array, hash, map?

Tue Sep 17, 2013 3:20 pm

Just did in the last post:
:local assoc {"a"="test1"}
:put [($assoc->"a")]
If you replace
"test1"
with a number, a time or Boolean value, it works as expected, but this doesn't, and unquoting the string doesn't help either, i.e.
:local assoc {"a"=test1}
:put [($assoc->"a")]
also outputs an empty line.

All of these are from within a script in "/system script" that is then "run"-ed from a Winbox terminal window, with the router being an x86 machine (in case that's relevant too).
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6111
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: is there any associative array, hash, map?

Tue Sep 17, 2013 3:33 pm

Thanks it is a bug, creates array of both elements (see :environment print)

We will fix this.
 
jonmansey
Frequent Visitor
Frequent Visitor
Posts: 72
Joined: Sat Sep 18, 2004 3:43 am

Re: is there any associative array, hash, map?

Mon Oct 07, 2013 11:54 pm

How do you set just one element of such a hash?

:global assoc {a=1;b=2;c=3};

:put ($assoc->"a")

1

:put ($assoc->"b")

2

env pr

assoc={a=1; b=2; c=3}


now i want to change the value of b

:set assoc {b=5}

:put ($assoc->"b")

5

but now when I look at the other rows, they are gone. the set has wiped out the other elements.

:put ($assoc->"a")

<blank>

env pr

assoc=


whats the correct set syntax to change only one element of a hash? Thanks
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: is there any associative array, hash, map?

Thu Nov 14, 2013 6:34 pm

How do you set just one element of such a hash?

:global assoc {a=1;b=2;c=3};

:put ($assoc->"a")

1

:put ($assoc->"b")

2

env pr

assoc={a=1; b=2; c=3}


now i want to change the value of b

:set assoc {b=5}

:put ($assoc->"b")

5

but now when I look at the other rows, they are gone. the set has wiped out the other elements.

:put ($assoc->"a")

<blank>

env pr

assoc=


whats the correct set syntax to change only one element of a hash? Thanks



Was there ever an answer to this? How do you set a single element in an array? or an associative array?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6111
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: is there any associative array, hash, map?

Fri Nov 15, 2013 1:24 pm

set overwrites value of variable, so in example you are setting array with one element
:set assoc {b=5}


Currently there is no command to change single element in array.
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: is there any associative array, hash, map?

Fri Nov 15, 2013 2:26 pm

set overwrites value of variable, so in example you are setting array with one element
:set assoc {b=5}


Currently there is no command to change single element in array.
Ugh... ok. Helping someone out with a project and the basically non-existent data structures and lack of atomic operations are a problem. I can't use pick and set since if the thread gets interrupted I will be hosed... some sort of semaphore/mutex or something along with push/pop and actual linked lists would be really useful.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6111
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: is there any associative array, hash, map?

Fri Nov 15, 2013 3:24 pm

We are planning to add commands to change individual array elements. Most likely in v6.7 you will have this feature.
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: is there any associative array, hash, map?

Fri Nov 15, 2013 4:14 pm

We are planning to add commands to change individual array elements. Most likely in v6.7 you will have this feature.
What about atomic operations?... right now if I have multiple threads attempting to access a single list (e.g. utilizing Traffic Monitors etc)... can I be guaranteed that the push/pop operations will not loose entries?

-Eric
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: is there any associative array, hash, map?

Fri Nov 15, 2013 5:21 pm

Personally, I'd prefer named locks instead of some sort of atomic flavored operations.

e.g.

ros code

:lock "nameOfMyLock" do={
# Stuff that must be atomic in relation to "nameOfMyLock".
# i.e. any other code featuring a ":lock" with "nameOfMyLock" will block at that point until THIS section is exited.

    :set "globalVariableCounter" ($globalVariableCounter + 1)

}
Ideally, there should also be an option for shared locks and a timeout... but most scripts would be fine even with just exclusive indefinite locks.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6111
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: is there any associative array, hash, map?

Tue Feb 04, 2014 4:33 pm

Now setting value to single array element also works

ros code

[admin@MikroTik] > :global rr { "apple"=12345; "peach"="abe" }                                    
[admin@MikroTik] > :set ($rr->"apple") "lala"                 
[admin@MikroTik] > :environment print 
rr={apple="lala"; peach="abe"}
 
efaden
Forum Guru
Forum Guru
Posts: 1711
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: is there any associative array, hash, map?

Tue Feb 04, 2014 4:43 pm

Now setting value to single array element also works

ros code

[admin@MikroTik] > :global rr { "apple"=12345; "peach"="abe" }                                    
[admin@MikroTik] > :set ($rr->"apple") "lala"                 
[admin@MikroTik] > :environment print 
rr={apple="lala"; peach="abe"}
How about some sort of mutex/lock/semaphore?
 
lukkes
Member Candidate
Member Candidate
Posts: 169
Joined: Mon Jun 16, 2008 2:12 am
Location: Venezuela
Contact:

Re: is there any associative array, hash, map?

Tue Mar 04, 2014 5:42 am

hi, is there a way to assign values to a variable array in ros 5.21 ? just ike this
{a=1;b=3}

thanks;
If you found this post useful don't forget about the karma viewtopic.php?f=1&t=41148

Feed Your FAITH Then Your Doubts Will Starve To Death...!!!
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6111
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: is there any associative array, hash, map?

Tue Mar 04, 2014 1:58 pm

No it doesn't work in v5.x
 
mfrater
newbie
Posts: 32
Joined: Wed Aug 14, 2013 2:11 am
Location: Auckland, New Zealand
Contact:

Re: is there any associative array, hash, map?

Wed Apr 02, 2014 7:40 am

Can I create an associative array as a global variable without defining the how many elements there will be in the array?

I can't create an array with zero elements, but if I define one or two, I appear to be able to add more that are not defined. But I also appear to have a NULL { } entry at the beginning of the array too.
[admin@seaview2] > :global a {}
syntax error (line 1 column 13)

[admin@seaview2] > :global a {x,y}
[admin@seaview2] > :environment print
a={{}}

[admin@seaview2] > :set ($a->"y") 2
[admin@seaview2] > :set ($a->"x") 5
[admin@seaview2] > :set ($a->"z") 6
[admin@seaview2] > :environment print
a={{}; x=5; y=2; z=6}

[admin@seaview2] >
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6111
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: is there any associative array, hash, map?

Wed Apr 02, 2014 11:18 am

ros code

:global myArray ;
:set $myArray {a=1}
:set ($myArray->"b") 2 

:environment print 
myArray={a=1; b=2}
 
mfrater
newbie
Posts: 32
Joined: Wed Aug 14, 2013 2:11 am
Location: Auckland, New Zealand
Contact:

Re: is there any associative array, hash, map?

Thu Apr 03, 2014 1:09 am

Thanks. That works!

If I :set $myArray {a=1} everytime my script runs, it will wipe all entries. I tried this, but it doesn't work.
# script 3
:global myArray;
# Initialise associative array with one key/value pair. Don't initialise it if array contains data, as it will wipe all other entries.
:if ($myArray="") do={
    :set $myArray {a=1}
    :put "myArray initialised";
   } else {
    :put "myArray NOT initialised";
   }
:put [/environment print]
Output:
> /system script run script3
myArray NOT initialised
myArray=[:nothing]
>
 
mfrater
newbie
Posts: 32
Joined: Wed Aug 14, 2013 2:11 am
Location: Auckland, New Zealand
Contact:

Re: is there any associative array, hash, map?

Thu Apr 03, 2014 1:42 am

ok.
This works.
:if ( [:len $myArray] =0 ) do={
 
jeroenp
Member Candidate
Member Candidate
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: is there any associative array, hash, map?

Thu Oct 13, 2016 9:32 am

Can I create an associative array as a global variable without defining the how many elements there will be in the array?
Yes you can; I asked this a while ago myself at http://forum.mikrotik.com/viewtopic.php ... 96#p549154
## set empty associative array: http://forum.mikrotik.com/viewtopic.php?p=418796#p549154
:local attackCounts ({});
Then use it like this:
    :local attackCount ($attackCounts->$attackIp);
    :local attackCountType [:typeof $attackCount];
    :if ($attackCountType = "num") do={
      :set $attackCount ($attackCount+1);
    } else={
      :set $attackCount 1;
    };
    :set ($attackCounts->$attackIp) ($attackCount);
--jeroen

Who is online

Users browsing this forum: eggbean and 10 guests