Sorry Jotne,
I'll try to be more clear.
I'm using a local variable to hold a dictionary. I'm expecting that local variable to be empty next time around, not hold values from previous runs.
I'm not interested in "doing" something. I've already done it. What I want is to understand why a local variable retains it's values, if this is intended (and why) or if this is a bug.
If you really really need a script, consider this from my previous post.
I have a global function that acts as a directory.
Within it I have names, emails, phones and maybe other info. Not all entries need to have the same keys.
Each time I request something I expect to get the proper values.
It's not about if the script is optimal, makes sense, can be written in a different way or whatever, it's about not producing the expected results and why.
script:
global main do={
local dict ({})
#populate dict with values based on given name
if ($1="bob") do={
set ($dict->"name") "bob"
set ($dict->"address") "maple st."
set ($dict->"mobile") "5551234"
}
if ($1="tom") do={
set ($dict->"name") "tom"
set ($dict->"mobile") "666"
set ($dict->"birthday") "01011970"
}
return $dict
}
results:
[kntouskos@sini2] > foreach key,value in=[$main bob] do={ put ("$key is $value") } #requesting dictionary for bob, as this is the first run results are correct.
address is maple st.
mobile is 5551234
name is bob
[kntouskos@sini2] > foreach key,value in=[$main tom] do={ put ("$key is $value") } #requesting dictionary for tom, results are incorrect
address is maple st. #tom has no address key, we're getting bobs
birthday is 01011970
mobile is 666
name is tom
[kntouskos@sini2] > foreach key,value in=[$main bob] do={ put ("$key is $value") } #3rd run, requesting bob again
address is maple st.
birthday is 01011970 #bob has no birdthday, we're getting toms
mobile is 5551234
name is bob
I do understand from your previous post that the function is getting re-written by each iteration, the question is why and should it.
[kntouskos@sini2] > put $main
;(eval / (eval /localname=$dict;value=address=maple st.;birthday=01011970;mobile=5551234;name=bob) (eval /ifcondition=(= $1 bob);do=;(eval / (eval /setname=(-> $dict name);value=bob) (eval /setname=(-> $dict address);value=maple st.) (eval /setname=(-> $dict mobile);value=5551234))) (eval /ifcondition=(= $1 tom);do=;(eval / (eval /setname=(-> $dict name);value=tom) (eval /setname=(-> $dict mobile);value=main ave.) (eval /setname=(-> $dict birthday);value=01011970))) (eval /returnvalue=$dict))