0

For some reason, the Maple code

testproc := proc()
  LOCAL abc;
  abc[1] := 123;
  print(eval(parse(cat("abc[", 1, "]"))))
end proc

testproc();

produces

abc_1

whereas (same, but with abc now a GLOBAL variable)

testproc := proc()
  GLOBAL abc;
  abc[1] := 123;
  print(eval(parse(cat("abc[", 1, "]"))))
end proc

produces (what I want)

123

What do I need to do so that I can evaluate a concatenated string involving a local variable? Many thanks for any help! :)

  • what language is this ? – jsedano Apr 03 '13 at 23:15
  • That's right; concatenation produces a global name. It's likely that you are asking how to implement a dubious method of... something that might well be better done another way. Why do you want to do this? It's not just about indexed names, right? Did you explicitly declare all such possible locals? How many? Why do they need to be formed via concatenation later on? Why not post a better representative of what you are actually trying to accomplish. This way seems misguided. – acer Apr 04 '13 at 04:57
  • Thank you Acer. The reason why I am doing this to create a list of task as a string to be executed via Threads. I don't know ahead of time how many branches I will get, so I simply collect them into a string. Of course I am open to better ways of doing this. – user2242610 Apr 04 '13 at 07:42
  • Well, why not declare `abc` as a local, and assign into it in an indexed manner , ie. `abc[i]:=...` in a usual way. If you need to return that table from the proc then just `return eval(abc)` so you can use it at the higher (global, say) scope. No need to attempt side-effects on `abc` as a declared global. – acer Apr 08 '13 at 19:54

1 Answers1

0

When you use parse, it operates as if the text was in its own file or entered at the top level. It doesn't have the context of lexically scoped variables.

You could do something like

eval(parse(cat("abc[",1,"]")),convert('abc',`global`)='abc');

If you want to handle multiple locals, use a set for the second argument to eval.

I assume you have some reason for going through the string form. For straight object manipulation, it isn't usually a good idea.

DrC
  • 7,528
  • 1
  • 22
  • 37