Skip to main content
Question

Schemamapper - do math

  • February 17, 2014
  • 5 replies
  • 48 views

sigtill
Supporter
Forum|alt.badge.img+25
Using the following within EXCEL or CSV-file works with STRINGS:

 

 

MYNEWVALUE = @Evaluate([return {@Value(MYATTRIBUTE1)_@Value(MYATTRIBUTE2)}])

 

 

However when I try to do arithmetic operations I only get the letters and not the calculations. Any ideas?

 

 

Doing this:

 

 

MYNEWVALUE = @Evaluate([return {@Value(MYNUMBERATT1)-@Value(MYNUMBERATT2)}])

 

 

With attributes

 

MYNUMBERATT1 = 10

 

MYNUMBERATT2 = 1

 

 

gives: MYNEWVALUE = 10-1

 

 

And NOT the desirec MYNEWVALUE = 9
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

5 replies

takashi
Celebrity
  • February 17, 2014
Hi Sig,

 

 

Try "expr" command instead of "return".

 

-----

 

@Evaluate([expr {@Value(MYNUMBERATT1)} - {@Value(MYNUMBERATT2)}])

 

-----

 

https://www.tcl.tk/man/tcl8.5/TclCmd/expr.htm

 

 

Takashi

takashi
Celebrity
  • February 17, 2014
Or just evaluate the expression. Probably this also works.

 

-----

 

@Evaluate(@Value(MYNUMBERATT1) - @Value(MYNUMBERATT2))

 

-----

sigtill
Supporter
Forum|alt.badge.img+25
  • Author
  • Supporter
  • February 17, 2014
Great help as always Takashi! 

gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • February 17, 2014
Tho Takashi is correct, [expr someexpression] to do expressions (lol) is the way to go,

 

 

u also could have added an @Evaluation over your new value, or added an "=" in front of it.

 

 

Because your result was an "unevaluated" expression. Wich is the result of using [return ..]

 

 

btw it is [expr { yourexpression}] You can save some braces.

takashi
Celebrity
  • February 18, 2014
Gio is right. Curly braces are not essential in this case, because MYNUMBERATT1 and MYNUMBERATT2  should be numeric representations and they cannot contain space characters. And the expression in my first post was no good. It would fail if the space behind - operator was omitted and MYNUMBERATT2 was negative (i.e. begins with minus sign).

 

These are right. Spaces before and behind - operator can be omitted.

 

-----

 

@Evaluate([expr {@Value(MYNUMBERATT1) - @Value(MYNUMBERATT2)}])

 

-----

 

@Evaluate([expr @Value(MYNUMBERATT1) - @Value(MYNUMBERATT2)])

 

-----

 

@Evaluate(@Value(MYNUMBERATT1) - @Value(MYNUMBERATT2))

 

-----

 

Thanks for the attention, Gio.

 

 

In addition, if the attribute values weren't numeric representations, the workspace would fail even though the expression was surrounded by curly braces. If it cannot be guaranteed that they are always numeric, you should validate them before math operation to prevent failure. Use the AttributeClassifier beforehand to filter out invalid features, or create conditional value with Tcl commands. For example:

 

-----

 

@Evaluate([if {[string is double -strict {@Value(MYNUMBERATT1)}] && [string is double -strict {@Value(MYNUMBERATT2)}]} {expr @Value(MYNUMBERATT1) - @Value(MYNUMBERATT2)} else {return "Invalid"}])

 

-----