EVAL IF ELSE, using text, not numbers

Support for iMacros. The iMacros software is the unique solution for automating every activity inside a web browser, for data extraction and web testing.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
Thrilling
Posts: 33
Joined: Fri Aug 11, 2017 2:02 pm

EVAL IF ELSE, using text, not numbers

Post by Thrilling » Mon Sep 18, 2017 2:35 pm

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
chivracq
Posts: 8919
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: EVAL IF ELSE, using text, not numbers

Post by chivracq » Mon Sep 18, 2017 3:05 pm

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.
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
Thrilling
Posts: 33
Joined: Fri Aug 11, 2017 2:02 pm

Re: EVAL IF ELSE, using text, not numbers

Post by Thrilling » Tue Sep 19, 2017 7:43 am

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
chivracq
Posts: 8919
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: EVAL IF ELSE, using text, not numbers

Post by chivracq » Tue Sep 19, 2017 11:10 am

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;")
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
Thrilling
Posts: 33
Joined: Fri Aug 11, 2017 2:02 pm

Re: EVAL IF ELSE, using text, not numbers

Post by Thrilling » Tue Sep 19, 2017 11:47 am

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}}
Post Reply