Community discussions

 
pe1chl
Forum Guru
Forum Guru
Topic Author
Posts: 5912
Joined: Mon Jun 08, 2015 12:09 pm

What is the working method to define and clear an array?

Thu May 17, 2018 2:40 pm

I need a working method to setup an array variable AND clear all the values.
I have tried many things but every time some aspect of it is not working.

I thought I had finally nailed it using this construct:

:global ArrayVariable;
:set $ArrayVariable ({});

This indeed sets up the variable as an empty array at first (e.g. after the router is booted) and I can
add new items to it using

:set ($ArrayVariable->$Index) -1;

($Index is an array index here, and -1 is the value the array item is set to)

However, when I again execute the

:set $ArrayVariable ({});

All the items are still in the array! So the new items are added to what is already there.
I want this to clear the existing items and start from scratch. How is it done?
 
pe1chl
Forum Guru
Forum Guru
Topic Author
Posts: 5912
Joined: Mon Jun 08, 2015 12:09 pm

Re: What is the working method to define and clear an array?

Tue May 22, 2018 1:14 pm

Nobody?
Is the only way to loop through the array indices and remove them one by one?
 
User avatar
dasiu
Trainer
Trainer
Posts: 232
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: What is the working method to define and clear an array?

Wed Jun 06, 2018 12:17 pm

There is a bug, I already reported it. I believe it should work like you presented.
Please, see this example script, it's even better :)
:local xxx {"Key 0"="Value 0"}
:put "Initial array:"
:put $xxx

:set ($xxx->"Key 1") "Value 1"
:put "Array after using set:"
:put $xxx

:set $xxx ($xxx,{"Key 2"="Value 2"})
:put "Array after concatenating:"
:put $xxx
Try writing it as a script, and then running from console. At first run it is correct, at second (and next) runs - the value set using :set ($xxx->"key") is remembered even after script finished (and the scope of the variable is local) :). When set using the second method (concatenating "old" array with new element) - it works. The only problem is that the "concatenation" doesn't work when the array key is a variable, not static text:
:set $ArrayVariable ($ArrayVariable,{"key"="-1"}); - should work
:set $ArrayVariable ($ArrayVariable,{"$Index"="-1"}); - my experience shows it doesn't work...

OH! In your case, if the variable is global, this can work (this is a workaround I found) :). Please try doing the set the following way:
:execute ":set \$ArrayVariable (\$ArrayVariable,{$Index=-1})";
I wonder if it also works for you :).
 
pe1chl
Forum Guru
Forum Guru
Topic Author
Posts: 5912
Joined: Mon Jun 08, 2015 12:09 pm

Re: What is the working method to define and clear an array?

Wed Jun 06, 2018 2:44 pm

I have no problems with setting the values in the array. The construct shown above does that reliably.
All my accesses to the array are using a variable for the index and it works fine.
I only have the problem that when I want to start with a clean slate, I see difficulty doing that.
The setting of the array to ({}) at first appears to clean the array, but when new values are then
added to it using :set ($array->$index) value; the old index values appear again (with the old values),

It cannot be reproduced from commandline. I only see this behaviour when using a global variable that
is accessed from a script that runs from the scheduler. But when I access the variable from a commandline
script I do see the old values, also in the /system script environment screen the old values are shown.
However removing the variable there or doing the set to ({}) from the commandline often appears to fix
the issue.

It is probably a bug. Hopefully it is the bug that you reported, and I hope it will be fixed sometime.
Fortunately in my application it rarely happens that values are to be deleted, so setting the array to
empty and then adding all the values usually works OK no matter what (i.e. when the clear does not
succeed, it is not really a problem). But of course something is not right about it.

Who is online

Users browsing this forum: No registered users and 14 guests