Sign in to follow this  
Followers 0
timmyc

Need help formatting IF AND THEN statement with multiple possibilities

18 posts in this topic

 

Hey All,

Thanks for your help, couldnt find any info on how to send multiple strings to check in an IF AND THEN statement.

Am trying to do an action only when a person is a certain ROLE and a certain LOCATION.

so...this is what i have however everything is coming back with the "It worked" Msgbox and i cannot get anything to throw the "Computer says no" error message.

Could somone lend a hand?

Thanks

 

if (($role = "1" OR "2" OR "3" OR "4" OR "5" OR "6" OR "7" OR "8") and ($loc ="Mackay" OR "MBH" OR "Royal Child" OR "RCH" OR "CBH" OR "Cairns" OR "CH")) then
   MsgBox(0,"WOOHOO","It worked")
Else
   MsgBox(0,"BOWBOWWWW","Computer says nooo")
EndIf

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

In this kind of cases you use switch function 

;click on "switch" to see the help file
switch $role
;...
EndSwitch

EDIT: I think is this what you looking for:

Switch $role
    Case 1 To 8
        Switch $loc
            Case "Mackay", "MBH", "Royal Child","RCH", "CBH", "Cairns", "CH"
                   MsgBox(0,"WOOHOO","It worked")
        EndSwitch
    Case Else
        MsgBox(0,"BOWBOWWWW","Computer says nooo")
EndSwitch

EDIT2: As JohnOne said, you can also do it with "if then" statements, but will look messy

if ($role = "1" OR $role = "2" OR $role = "3" OR $role = "4" OR $role = "5" OR $role = "6" OR $role = "7" OR $role = "8") and ($loc = "Mackay" OR $loc = "MBH" OR $loc = "Royal Child" OR $loc = "RCH" OR $loc = "CBH" OR $loc = "Cairns" OR $loc = "CH") then
   MsgBox(0,"WOOHOO","It worked")
Else
   MsgBox(0,"BOWBOWWWW","Computer says nooo")
EndIf

AutoIt has a line length limit, so avoid make it too long using " _" for line breaks on code, "& _" for text breaks.

Ex:

if ($role = "1" OR $role = "2" OR $role = "3" OR $role = "4" OR $role = "5" OR $role = "6" OR _
      $role = "7" OR $role = "8") and ($loc = "Mackay" OR $loc = "MBH" OR $loc = "Royal Child" OR $loc = "RCH" _
      OR $loc = "CBH" OR $loc = "Cairns" OR $loc = "CH") then
   MsgBox(0,"WOOHOO","It worked")
Else
   MsgBox(0,"BOWBOWWWW","Computer says nooo")
EndIf
Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

timmyc, the returned text from WinGetText(), needs to be exactly one of those you specified, if has a single space or dot it won't match.

Do a dump of all possible values (verify also if they are always like that), and add them by copy&paste.

like:

ConsoleWrite('"'&WinGetText("test1")&'"'&@LF) ;will write the output of wingetext enclosed in quotes ("") to autoit/scite console
Exit

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Thanks John I noticed my mistake as soon as i posted it & Kyan I cant get that far to run that command.
 

I noticed i was pulling the wrong Location details so i changed the StringRegExp to look for the appropriate fields but am now getting this error

Subscript used on non-accessible variable
$role = ( $aRes[0] )
$role = ( $aRes^ ERROR

I am thinking that it cannot build the array and i think it is because of the brackets after "Please enter your primary place of work" 

Also note i do not know how to proply format the StringRegExp command, i replicated what i had got working.

$text = WinGetText(($sc)& " - Service Call - New")
$text = ControlGetText(($sc)& " - Service Call - New", "", "[CLASS:WFC.EDIT; INSTANCE:24]") ; or only with the control

$aRes = StringRegExp($text, "(?is)AccessRequired : (\N+).*Please enter your primary place of work (RCH, MBH, CH, Pathology Qld) : (\N+).*Given name : (\N+).*Surname : (\N+).*Email address : (\N+).*cn=([^,]+)", 3)

$role = ( $aRes[0] )
$loc = ( $aRes[1] )
$name = ( $aRes[2] )
$lname = ( $aRes[3] )
$email = ( $aRes[4] )
$nov = ( $aRes[5] )

Share this post


Link to post
Share on other sites

use consolewrite to debug your code. Right after $ares = stringregexp...., write ConsoleWrite("ERROR: "&@Error&@TAB&"Extended: "&@Extended&@LF)

It will show the error code and extended error for the last command (stringregexp).

Click on stringregexp (leaving the blinking editor "|") and hit F1, it will show you the help file of stringregexp, scroll down and you will find the error's codes for that function.

After you see whats wrong guess will be easy to fix :)

Local $aRes
$text = ControlGetText(($sc)& " - Service Call - New", "", "[CLASS:WFC.EDIT; INSTANCE:24]") ; or only with the control

if text <> '' then
   $aRes = StringRegExp($text, "(?is)AccessRequired : (\N+).*Please enter your primary place of work (RCH, MBH, CH, Pathology Qld) : (\N+).*Given name : (\N+).*Surname : (\N+).*Email address : (\N+).*cn=([^,]+)", 3)

   $role = $aRes[0]
   $loc = $aRes[1]
   $name = $aRes[2]
   $lname = $aRes[3]
   $email = $aRes[4]
   $nov = $aRes[5]
EndIf

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

That error code fuction was really handy! Thanks for that tip.

Through troubleshooting i have found out what was causing the issue... the brackets in "Please enter your primary place of work (RCH, MBH, CH, Pathology Qld) :"

I couldnt remove them from the template i was using so i used the Q and E commands for StringRegExp which worked!!!

Thanks for your help!

Share this post


Link to post
Share on other sites

Hi,
Last question, and i hope its an easy one.

What i have:
$role = "Practitioner"

What i need
$role (contains) "Practitioner"

At the moment my script is only returning results for $role = "Practitioner" however some 'chunks' of data may come through with "Nursing - Practitioner".

Is there a fast easy way to turn the = into contains?

Share this post


Link to post
Share on other sites

timmyc, you could use John's suggestion, or do it with a regex since its getting complicated

like so:

if StringRegExp($role,"(?i)[1-8]|.*?Practitioner.*?") and StringRegExp($loc,"(?i)Mackay|MBH|Royal Child|RCH|CBH|Cairns|CH") then
    MsgBox(0,"WOOHOO","It worked")
Else
    MsgBox(0,"BOWBOWWWW","Computer says nooo")
EndIf

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Thanks a lot guys, i got it working with multiple Stringinstr commands its very messy...but i will try tidy it up with a regex down the track.

John your Beginners link was helpful by the way.

Speaking of beginners, i have a Dim/Local/Global issue where a variable in the first func in not coming over to the second func. I know this is stupidly basic but i have tried putting Dim $eg, Local $eg and Global $eg above the second func (and below the func line just for good measure) however it does not send the variable, it either errors at the send($eg) line or it send nothing and keeps running.

Looked in here and it gets a bit overwelming (im a noob) and i cannot find a applicable example

https://www.autoitscript.com/wiki/Best_coding_practices
https://www.autoitscript.com/autoit3/docs/keywords/Dim.htm

Share this post


Link to post
Share on other sites
if (($role = "1" OR "2" OR "3" OR "4" OR "5" OR "6" OR "7" OR "8") and ($loc ="Mackay" OR "MBH" OR "Royal Child" OR "RCH" OR "CBH" OR "Cairns" OR "CH")) then
   MsgBox(0,"WOOHOO","It worked")
Else
   MsgBox(0,"BOWBOWWWW","Computer says nooo")
EndIf

 

also using BitOR() could do

$Result1 = BitOR($role = "1", $role = "2", $role = "3", $role = "4", $role = "5", $role = "6", $role = "7", $role = "8")
;
$Result2 = BitOR($loc = "Mackay", $loc = "MBH", $loc = "Royal Child", $loc = "RCH", $loc = "CBH", $loc = "Cairns", $loc = "CH")
;
If $Result1 And $Result2 Then
    MsgBox(0, "WOOHOO", "It worked")
Else
    MsgBox(0, "BOWBOWWWW", "Computer says nooo")
EndIf

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

global $sc
func Runjob()


$text = ControlGetText(($sc)& " - Service Call - New", "", "[CLASS:WFC.EDIT; INSTANCE:24]")
$aRes = StringRegExp($text, "(?is)AccessRequired : (\N+).*\Q Pathology Qld) : \E(\N+).*Given name : (\N+).*Surname : (\N+).*Email address : (\N+).*cn=([^,]+)", 3)


$role = ( $aRes[0] )
$loc = ( $aRes[1] )
$name = ( $aRes[2] )
$lname = ( $aRes[3] )
$email = ( $aRes[4] )
$nov = ( $aRes[5] )


EndFunc


Global $nov, $role, $loc, $name, $lname, $email, $sc
func hna()


Winwaitactivate("Untitled - Notepad")
Send($nov)

Thanks for your help Chimp, im having a really basic issue with declaring variables. I have simplified the text above for troubleshooting purposes but basically the second func does not know what $nov is. I need to split them up (into separate func's) because i will need to repeat one func and not the other. I though you had to tell any new func the variables that where set outside of that func in order to use them...i think i am missing something so so obvious

Share this post


Link to post
Share on other sites

since you declared $nov as a Global variable, it should be visible also within Functions.

why you say "the second func does not know what $nov is"?
when you do  this $nov = $aRes[5] are you sure that the $aRes[5] variable contains data?

also, use Global at the beginning of the script

Global $nov, $role, $loc, $name, $lname, $email, $sc
Global $sc

Func Runjob()
    $text = ControlGetText(($sc) & " - Service Call - New", "", "[CLASS:WFC.EDIT; INSTANCE:24]")
    $aRes = StringRegExp($text, "(?is)AccessRequired : (\N+).*\Q Pathology Qld) : \E(\N+).*Given name : (\N+).*Surname : (\N+).*Email address : (\N+).*cn=([^,]+)", 3)

    $role = $aRes[0]
    $loc = $aRes[1]
    $name = $aRes[2]
    $lname = $aRes[3]
    $email = $aRes[4]
    $nov = $aRes[5]
EndFunc   ;==>Runjob

Func hna()
    WinWaitActive("Untitled - Notepad")
    Send($nov)
EndFunc   ;==>hna

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

 

since you declared $nov as a Global variable, it should be visible also within Functions.

Great, so my understanding of Global is somewhat correct

why you say "the second func does not know what $nov is"?

First Func was 'runjob', second func was 'hna'. I mean the second func (HNA) did not know what the variables were eg. $nov

when you do  this $nov = $aRes[5] are you sure that the $aRes[5] variable contains data?

I thought about that but verified that it is pulling the right data

 

also, use Global at the beginning of the script

This fixed it but i am unsure why...I moved the whole global line to the top of the script and it worked. I thought, for the variable to work, it needed to be declared anywhere before the Func that it will be used in. Is it normal to have to declare a variable before it is actually set?

Share this post


Link to post
Share on other sites

 

 

 

from this link:

remarks:

"1. Declare a variable before you use it (similar to VBScript)".

 

I think that, since the $nov variable was declared as Global after that it was used within the first function, it was implicitly considered as local in the first function causing to be two distinct variables with the same name, one local within the first function and the other is the global one.... maybe


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

Thanks Chimp, I knew you had to declare a value before you use it, i did not know you had to declare it before you set it which seems to have been the issue here.

Thanks again for your help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0