eval() macro as part of the nedit macro language

Bert Wesarg wesarg at informatik.uni-halle.de
Tue Jan 23 13:15:53 CET 2007


>>> OTOH, how about (nedit):
>>>   test = "hello"
>>>   dialog(eval("return { ." test " = \"world\" }").hello)
>>>              # displays "world"
>>>
>> Mhh, shouldn't this be like this:
>> dialog(
>>   eval(
>>     "return eval(\"return { ." test " = \\\"world\\\" }\")." test ")"
>>   )
>> )
>>
> No: the string seen by eval should be
>   return { .hello = "world" }
> which, quoted, becomes
>   "return { .hello = \"world\" }"
> now, the word "hello" comes as the content of variable test, so use
                ^^^^^^^
> concatenation to build the string:
>   "return { ." test " = \"world\" }"
> so now eval() will return an array with content ["hello"] = "world";
> using the .index syntax I mentioned, we extract the content directly
> from the eval() return value:
>   eval("return { ." test " = \"world\" }").hello
                                             ^^^^^
but here you must 'know' the content of the variable test.
with the outer eval() you even don't need to know this.
So starting with this:
  return eval("return { .hello = \"world\" }").hello
which, quoted, becomes
  "return eval(\"return { .hello = \\\"world\\\" }\").hello"
and with the content of test:
  "return eval(\"return { ." test " = \\\"world\\\" }\")." test ""
becomes this:
  eval("return eval(\"return { ." test " = \\\"world\\\" }\")." test)

so indeed my first fix was wrong (compare the ends). but in the end, its
quite a constructed example ;-). Than, if the "world" is what you want,
its either constant, or in an other variable, that you can excess directly:
  tset = "world"
  tset == eval("return eval(\"return { ." test " = \\\"" tset "\\\" }\")."
test)

> which is the single argument to dialog() in my code.
> 
> Tony

Bert


More information about the Develop mailing list