Page 1 of 1

EVAL IF ELSE, using text, not numbers

Posted: Mon Sep 18, 2017 2:35 pm
by Thrilling
Hello,

I'm using iMacros 8.9.7 on FIrefox 45.9.0 ESR on Solaris 11.3 (x64, german)

It's a simple problem I'm missing the syntax to. I'd like imacros to check if theres a certain word in the .csv, and to choose an option accordingly.

the following does not work:
SET !var2 EVAL("var x = \"{{!col2}}\"; if(x=no){x=0;} else{x=1;}; x;")

I tried several characters to put around "no" but had no luck and/or googlefu

Re: EVAL IF ELSE, using text, not numbers

Posted: Mon Sep 18, 2017 3:05 pm
by chivracq
Thrilling wrote:Hello,

I'm using iMacros 8.9.7 on FIrefox 45.9.0 ESR on Solaris 11.3 (x64, german)

It's a simple problem I'm missing the syntax to. I'd like imacros to check if theres a certain word in the .csv, and to choose an option accordingly.

the following does not work:

Code: Select all

SET !var2 EVAL("var x = \"{{!col2}}\"; if(x=no){x=0;} else{x=1;}; x;")
I tried several characters to put around "no" but had no luck and/or googlefu
This one should work, I think...:

Code: Select all

SET !VAR2 EVAL("var x='{{!COL2}}'; var z; if(x=='no'){z=0;} else{z=1;}; z;")
PROMPT COL2:<SP>_{{!COL2}}_<BR>VAR2:<SP>_{{!VAR2}}_
=> You need to double the '=' Sign in the 'IF' part and the Argument needs to be enclosed in Single Quotes (or escaped Double Quotes), unless you were using some Var that you've declared before in the 'EVAL()' Statement.

Re: EVAL IF ELSE, using text, not numbers

Posted: Tue Sep 19, 2017 7:43 am
by Thrilling
Thanks, it works for "no" now, but i also need it to recognize several words:

SET !VAR2 EVAL("var x='{{!COL1}}'; var z; if(x=='no'){z=0;} else{z=1;} ; if(x=='No'){z=0;} else{z=1;}; z;")

When i do this, it works for "No", but not for "no". i can only theorize, that the second IF ELSE overwrites the value of "z" of the first IF ELSE

so I tried this
SET !VAR2 EVAL("var x='{{!COL1}}'; var z; if(x=='no'); if(x=='No') {z=0;} else{z=1;}; z;")
but no success

I'm sure theres a simple way to write the syntax to solve this

Re: EVAL IF ELSE, using text, not numbers

Posted: Tue Sep 19, 2017 11:10 am
by chivracq
Thrilling wrote:Thanks, it works for "no" now, but i also need it to recognize several words:

Code: Select all

SET !VAR2 EVAL("var x='{{!COL1}}'; var z; if(x=='no'){z=0;} else{z=1;} ; if(x=='No'){z=0;} else{z=1;}; z;")
When i do this, it works for "No", but not for "no". i can only theorize, that the second IF ELSE overwrites the value of "z" of the first IF ELSE

so I tried this

Code: Select all

SET !VAR2 EVAL("var x='{{!COL1}}'; var z; if(x=='no'); if(x=='No') {z=0;} else{z=1;}; z;")
but no success

I'm sure theres a simple way to write the syntax to solve this
Yep, normal indeed and your Explanation is correct about the 2nd 'IF ELSE' "overwriting the 1st one as it is seen by the JS Runtime Engine as a separate Statement, you need to use 'IF ELSE IF ELSE IF ELSE...' in one single Statement...:

Code: Select all

SET !VAR2 EVAL("var x='{{!COL1}}'; var z; if(x=='no'){z=0;} else if(x=='No'){z=0;} else{z=1;}; z;")
And you should have a look a some JavaScript Command Reference, like this Site that I use to understand a bit the basic Use of Vars / 'IF ELSE' Conditions and Operators...

=> This one would work as well with a 'OR'...:

Code: Select all

SET !VAR2 EVAL("var x='{{!COL1}}'; var z; if((x=='no')||(x=='No')){z=0;} else{z=1;}; z;")
And you could as well use 'toLowerCase()' to make sure you only need 1 single 'IF ELSE' Statement which then covers the 4 possible Combinations "NO/No/nO/no"...:

Code: Select all

SET !VAR2 EVAL("var x='{{!COL1}}'; var z; if((x.toLowerCase())=='no'){z=0;} else{z=1;}; z;")

Re: EVAL IF ELSE, using text, not numbers

Posted: Tue Sep 19, 2017 11:47 am
by Thrilling
Works like a charm.

Thank you very much for the different approaches, even though my questions are very "newbie"ish :oops:
I'll make sure to reference the site you linked, in the future :!:

Code: Select all

VERSION BUILD=8970419 RECORDER=FX
TAB T=1
SET !DATASOURCE_DELIMITER ;
SET !DATASOURCE Russland.csv
SET !DATASOURCE_LINE 6
SET !ERRORIGNORE YES


SET !VAR2 EVAL("var x='{{!COL1}}'; var z; if((x=='no')||(x=='No')){z=0;} else{z=1;}; z;")
TAG POS=1 TYPE=SELECT FORM=ID:aspnetForm ATTR=ID:ctl00_phBody_StepViewMPersonalDetailsStep_Surnames_Condition_List CONTENT=%0
wait seconds=0.5
TAG POS={{!var2}} TYPE=SELECT FORM=ID:aspnetForm ATTR=ID:ctl00_phBody_StepViewMPersonalDetailsStep_Surnames_Condition_List CONTENT=%1
wait seconds=0.5
TAG POS={{!var2}} TYPE=INPUT:TEXT FORM=ID:aspnetForm ATTR=ID:ctl00_phBody_StepViewMPersonalDetailsStep_Surnames_ExNamesTable_ExName1_TextBox CONTENT={{!col1}}