3.3.5: About variable scopes

  • If keys only consist of numbers, then:
value=${"arg "$key",item1,item2,item3,item4,item5"}

will work (with key starting from 1).

  • If strings are allowed for keys, something as:
# dict_init : key1,value1,key2,value2,...,keyN,valueN
dict_init : $=arg repeat $#/2 { _dict_${"strvar \""${arg{2*$>+1}}\"}=${arg{2*$>+2}} }

# dict_get : key
dict_get : u ${_dict_${"strvar \"$1\""}}

# Example of use :
foo :
  dict_init "A flower","Rose","An animal","Fish","Pi",3.1415
  key="An animal"
  e "Value of key '"$key"' is '"${"dict_get \""$key\"}"'."

@Reptorian @David_Tschumperle
I see. Is it possible to change the value through it’s key?

In my example, just do : dict_init "An animal","Lion"

Lol yes of course, sorry for asking.

@Reptorian (next post below) Ah, i see. I’m really tired it seems.

Just started learning a bit of Python. I still don’t know the advantage of using a dictionnary instead of variables though. To iterate through it, print every values?
I guess i’ll know when i’ll have to use them.

For my example, it’d be just d$key=bar .

Dictionary allows you to retrieve a value at time complexity of ~O(1) though it takes space complexity. In my case, it simplified a code a bit by eliminating redundant arg by calling a variable which is O(1).

The method provided by David, and myself are the only reliable way of making dictionary with G’MIC. I wish the math parser supports something akin to dictionary, but it’s not there. And set() for that matter, but it’s not there.

Not exactly. In G’MIC, access to variable values indeed uses a hashmap, and accessing a variable value is indeed in O(1) when the corresponding hashmap slot contains a single value.
But it can be higher when the slot contains multiple variables.
FYI, G’MIC hashmap for storing variables has 2048 slots.

You can define dictionnary in the math parsers using G’MIC variables exactly the same way as in the example I’ve shown above. Just use get() and set() functions to retrieve or set variable values.

Just found out how.

$ echo {m='abc';set([m,v2s(123,5,10)],50);0;} echo $abc123

I’m guessing __ would allow dictionary along multiple thread I think.

Ok, I think I might have something here.

This new feature wouldn’t work with in case of parallel:

foo:
a,b=3,4
parallel _foo_1,_foo_2
echo $a,$b,$c,$d
_foo_1:
c:=$a*2
_foo_2:
d:=$b*2

Works fine if you separate them.

The reason I wanted to do is to try to parallelize a piece of +pal code. Particularly that long section, and I split them into 4 to assign variables using parallelization.

Let me see what I can do here. Appending __ to variables works though that’s not ideal because you’re still creating globals here, which avoiding this scenario is the purpose of this new feature.

Could be nice to bypass it though. Something like parallel_unsafe to ignore this issue.

EDIT: I succeeded in parallelization and it was a bad idea as in it made the code so much slower, but maybe this is still worth keeping as a post. I don’t know.