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.

Moderators: Community Moderators, iMacros Moderators

Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the Google search box (at the top of each forum page) to see if a similar problem or question has already been addressed. This will search the entire contents of the forums as well as the iMacros Wiki.
3. We can respond much faster to your posts if you include the following information:

CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST

Answering your own posts (e.g. attempting to "bump" your topic) drops your topic from the list of unanswered threads, so it may actually receive less views.

EVAL IF ELSE, using text, not numbers

by Thrilling on Mon Sep 18, 2017 7:35 am

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
Thrilling
 
Posts: 20
Joined: Fri Aug 11, 2017 7:02 am

Re: EVAL IF ELSE, using text, not numbers

by chivracq on Mon Sep 18, 2017 8:05 am

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)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
chivracq
 
Posts: 6474
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

Re: EVAL IF ELSE, using text, not numbers

by Thrilling on Tue Sep 19, 2017 12: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
Thrilling
 
Posts: 20
Joined: Fri Aug 11, 2017 7:02 am

Re: EVAL IF ELSE, using text, not numbers

by chivracq on Tue Sep 19, 2017 4: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)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
chivracq
 
Posts: 6474
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

Re: EVAL IF ELSE, using text, not numbers

by Thrilling on Tue Sep 19, 2017 4: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}}
Thrilling
 
Posts: 20
Joined: Fri Aug 11, 2017 7:02 am


Return to General Support & Discussions

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 9 guests

-->