문제

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! :)

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top