eval and inline expressions

Post your questions and help other users.

Moderator: Martin

Post Reply
Johannes
Posts: 1
Joined: 13 Dec 2014 12:22

eval and inline expressions

Post by Johannes » 13 Dec 2014 12:40

Hello Forum,

I try to replace a part of a string by using an inline expression and the eval function. I'm not sure, if this should work at all, but when I run the following script:

gpx_metadata = '"<metadata>\n<name>{gpslogger_file}</name></metadata>"';
log(eval(gpx_metadata));

I get this error:

13.12.2014 13:19:27.160 [recordLocation] Action 'Script: log(eval(gpx_metadata));'
ch.gridvision.ppam.androidautomagic.simplelang.a.d: Could not evaluate expression '"<metadata>
<name>{gpslogger_file}</name></metadata>"' due to errors: [Unclosed string[0-11], ';' expected[35], expression expected[35], ';' expected[35], arithmetic additive expression expected[35], arithmetic additive expression expected[41], ';' expected[42], expression expected[42], ';' expected[42], arithmetic additive expression expected[42], Unclosed string[52-53]]
at ch.gridvision.ppam.androidautomagic.simplelang.a.k.a(SourceFile:838)
at ch.gridvision.ppam.androidautomagic.simplelang.a.k.a(SourceFile:808)
at ch.gridvision.ppam.androidautomagic.simplelang.a.c.a(SourceFile:2152)
at ch.gridvision.ppam.androidautomagic.simplelang.a.b.a(SourceFile:659)
at ch.gridvision.ppam.androidautomagic.simplelang.c.ae.b(SourceFile:49)
at ch.gridvision.ppam.androidautomagic.simplelang.c.b.a(SourceFile:40)
at ch.gridvision.ppam.androidautomagic.simplelang.a.b.a(SourceFile:95)
at ch.gridvision.ppam.androidautomagic.simplelang.c.t.b(SourceFile:22)
at ch.gridvision.ppam.androidautomagic.simplelang.a.b.a(SourceFile:656)
at ch.gridvision.ppam.androidautomagic.simplelang.c.ae.b(SourceFile:49)
at ch.gridvision.ppam.androidautomagic.simplelang.c.b.a(SourceFile:40)
at ch.gridvision.ppam.androidautomagic.simplelang.a.b.a(SourceFile:95)
at ch.gridvision.ppam.androidautomagic.simplelang.c.t.b(SourceFile:22)
at ch.gridvision.ppam.androidautomagic.simplelang.a.b.a(SourceFile:675)
at ch.gridvision.ppam.androidautomagic.simplelang.c.v.b(SourceFile:42)
at ch.gridvision.ppam.androidautomagic.simplelang.c.b.a(SourceFile:40)
at ch.gridvision.ppam.androidautomagic.simplelang.a.b.a(SourceFile:102)
at ch.gridvision.ppam.androidautomagic.simplelang.c.ai.b(SourceFile:33)
at ch.gridvision.ppam.androidautomagic.simplelang.a.k.a(SourceFile:827)
at ch.gridvision.ppam.androidautomagic.simplelang.a.k.a(SourceFile:808)
at ch.gridvision.ppam.androidautomagic.d.a.dx$1.d(SourceFile:132)
at ch.gridvision.ppam.androidautomagic.d.a.dx$1.a(SourceFile:121)
at ch.gridvision.ppam.androidautomagic.util.hb$1.run(SourceFile:39)
at java.lang.Thread.run(Thread.java:841)

13.12.2014 13:19:27.174 [recordLocation] End executing action 'Script: log(eval(gpx_metadata));' and exception Could not evaluate expression '"<metadata>
<name>{gpslogger_file}</name></metadata>"' due to errors: [Unclosed string[0-11], ';' expected[35], expression expected[35], ';' expected[35], arithmetic additive expression expected[35], arithmetic additive expression expected[41], ';' expected[42], expression expected[42], ';' expected[42], arithmetic additive expression expected[42], Unclosed string[52-53]]


Obviously metadata is part of an xml file and my intention is to declare some template variables, which will be expanded by real values later on. Hopefully someone can give me a hint what I'm doing wrong.

Best Regards,

Johannes

User avatar
kintrupf
Posts: 257
Joined: 10 Sep 2013 08:59

Re: eval and inline expressions

Post by kintrupf » 15 Dec 2014 15:01

Johannes wrote:I try to replace a part of a string by using an inline expression and the eval function.
If you want to replace a part of a string, why don't you use the Replace function?

String replace(String s, String search, String replace)
Returns a modified string by replacing all occurrences of search with replace in string s.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: eval and inline expressions

Post by Martin » 15 Dec 2014 18:07

Hi,

The single quotes only prevent Automagic from interpreting inline expressions in curly braces but will not prevent the new line (\n) to be applied to the string.
This means that the string '"<metadata>\n<name>{gpslogger_file}</name></metadata>"' will look like this when passed to the eval-function:
"<metadata>
<name>{gpslogger_file}</name></metadata>"

This is not a valid script since the first line opens a string literal but does not close the string literal.

You could either use the replace-function as proposed by kintrupf or use one more backslash to escape the new line: '"<metadata>\\n<name>{gpslogger_file}</name></metadata>"' so that eval sees "<metadata>\n<name>{gpslogger_file}</name></metadata>".

Regards,
Martin

Post Reply