List Variables Are Pointers, Not True Variables

Post your questions and help other users.

Moderator: Martin

Post Reply
98b427af

List Variables Are Pointers, Not True Variables

Post by 98b427af » 09 Jan 2014 20:14

Take a look at the attached flow. In the first assignment, variables a and b end up pointing to the same list (i.e. the same memory location). Is that the intended behavior? It's a little counterintuitive, especially if you use setValue("b", getValue("a", null)) instead of the assignment operator. I expect both to make b contain a separate copy of the list, not point to the same list.

Is this documented anywhere? I couldn't find it. Thanks.

http://automagic4android.com/flow.php?i ... 30f53e6257

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: List Variables Are Pointers, Not True Variables

Post by Martin » 09 Jan 2014 20:44

Yes, this is the intended behavior. A variable is a reference (like a pointer but you can not do pointer arithmetic). Assigning a list within a variable to another variable does not automatically create a copy of the list.

Function x=getValue("a", null) returns the value stored in variable a and is more or less the same as executing x=a. The getValue has the slight advantage to access dynamically built variables like this:
b = "test"
x = getValue("x"+b, null); //to access variable xtest

It's not documented yet, I will add a short paragraph.

98b427af

Re: List Variables Are Pointers, Not True Variables

Post by 98b427af » 30 Jan 2014 19:38

What, then, is the recommended way to make a copy of a list? I guess it would have to be a for-loop. And I can't think of any way at all to do that for a map without knowing its keys ahead of time.

Also, is there any way to acquire a variable's (or a list element's or map entry's) type? For example, if I create a map "{a=1,b=2}" and a string "{a=1,b=2}", I can treat the map as a string but I can't treat the string as a map. I'd like to write a script that nicely formats variables for debugging but without knowing types ahead of time there would be a lot of complex parsing involved.

98b427af

Re: List Variables Are Pointers, Not True Variables

Post by 98b427af » 31 Jan 2014 05:24

Here's a script snippet that creates two maps that cannot be distinguished by looking at them in a Debug Dialog:

map1 = newMap();
b = "s";
addMapEntry(map1, "b=b, r", b);
map2 = newMap();
b = "b";
r = "s";
addMapEntry(map2, "b", b);
addMapEntry(map2, "r", r);

Without a way to extract a map's keys, there's no way to tell the two apart.

I'd like to request some new functions:

List getMapKeys(Map aMap)

It would return a List containing all the keys for the given map.

Also:

String typeOf(String variableName)

It would return the data type of the variable named "variableName".

And:

Map copyMap(Map aMap)
List copyList(List aList)

For duplicating Maps and Lists.

Just for your consideration. Thanks!

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: List Variables Are Pointers, Not True Variables

Post by Martin » 31 Jan 2014 17:45

Good idea, I add the functions to the todo-list.

Thanks & Regards,
Martin

Post Reply