#Error when using if statement inside foreach block
1 messages · Page 1 of 1 (latest)
[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.
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
It works fine when there is no if() statement.
All tokens have the Lv property assigned.
[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.
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
Yes, I've already tried that. I thought the syntax might be the issue, so I tried other variations as well, but it always breaks as soon as I use if().
So you have shown the output of the getProperty and it's what you expect?
Yes, the getProperty returns the expected value as intended. However, I'm not sure if it's just an issue on my end, but whenever I use if(), it doesn't produce the correct output.
Well, if you are sure then I can't help you
Although it hasn’t been resolved, thank you for your response...
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.
All of them return 1. However, it definitely seems like there's an issue with this getLv property. It appears that using it directly causes a problem.
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!
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!!
As you can see, there were no spaces in between. I've never encountered something like this before, but fortunately it’s been resolved.