#Error when using if statement inside foreach block

1 messages · Page 1 of 1 (latest)

mint ravine
#

When I put an if statement inside a foreach block, it doesn't work and throws an error, as shown in the screenshot.

I recently upgraded from an older version where this used to work, so I don't understand why it's no longer functioning.

I'm currently using version 1.15.2.

#

[h:ids=getTokens()]
[h:sence=0]

[h, foreach(id, ids, ""), CODE: {
[h:getLv=getProperty("Lv", id)]
[if(getLv >= 2), code: {[sence=sence+1]};{}]
}]

Two level [r:sence].

macro is tihs.
I had uploaded a test macro earlier, so I've now replaced it with the correct working version.

lyric river
#

You don't need a code block for one statement. It can just be [if(getLv>=2): sence2=1]

The error is probably because the "Lv" property is not properly set in one or more tokens

mint ravine
#

[h:ids=getTokens()]
[h:sence=0]

[h,foreach(id, ids, ""), CODE: {
[getLv=getProperty("Lv", id)]
[if(getLv >= 2): sence=sence+1]
}]

Two level [r:sence].

Even with the above macro, it still doesn’t work.

lyric river
#

It probably works without the if because it thinks it is doing an illegal comparison, like comparing a string to a number. Which indicates the property is not set properly. As a test you can remove the if and show the output of the getProperty and see if it's in fact what you expect

mint ravine
lyric river
#

So you have shown the output of the getProperty and it's what you expect?

mint ravine
lyric river
#

Well, if you are sure then I can't help you

mint ravine
balmy mason
#

It sounds like the value stored in that property should be numeric. What happens when you replace the original IF stmt with this?

[isNumber(getLv)]

You should get a string of 0's and 1's -- a zero when the result is false, and a one when the result is true. If you get ANY zeroes, one of the tokens has a problem. (Likely a lib: token or some other token that you've forgotten about!) Remember: getTokens() retrieves ALL of them since you haven't provided any filtering criteria.

mint ravine
mint ravine
#

But that gave me a hint and helped me solve it. I didn't realize the value from getProperty would be a string. Using number() fixed it. Thank you for pointing it out!

balmy mason
#

Ah. getProperty always returns strings, but MT generally does a good job of figuring out context and converting data types back and forth. It must not have figured it out for your IF stmt and compared getLv against “2” rather than using numbers…? Weird. Any ch ace your getLv had leading spaces in it?

#

Anyway, glad you got it figured out!!

mint ravine
#

As you can see, there were no spaces in between. I've never encountered something like this before, but fortunately it’s been resolved.