Jump to content
Sign in to follow this  
dreamzboy

How Many Variables can "IF" Statement Compare?

Recommended Posts

dreamzboy

I have 3 input boxes and I wanted to make sure all the entries entered in are equal before proceeding with the script.

The statement that I used

If ($host_Count = $ip_Count = $pw_Count)

only compared the first 2 variables and "ignored" the last variable, $pw_Count.  The script will execute regardless of what value $pw_Count holds.  Same thing happened if I were to use "AND" instead of equal sign.  How can I compare all 3 variables to make sure they all matches?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <GuiListBox.au3>
#include <array.au3>

Opt ("TrayIconDebug", 1)

Local $ribcl_temp = '', $xml_temp, $i
Local $hn_input, $ip_input, $pw_input
Local $host_Count, $ip_Count, $pw_Count
Local $msg, $success, $password, $hostname, $ip_address
Local $log_label, $generate_btn, $clrLog_btn
Local $test_str0 = "dl360g7-25" & @CRLF & "dl360g7-26" & @CRLF & "dl360g7-27"
Local $test_str1 = "10.112.15.22" & @CRLF & "10.112.15.23" & @CRLF & "10.112.15.24"
Local $test_str2 = "pass1" & @CRLF & "pass2" & @CRLF & "pass3"

GUICreate ("Test", 600, 450)

GUICtrlCreateLabel ("1.  Enter hostname (ex: dl360g7-25):", 30, 70, 250, 20)
GUICtrlSetFont (-1, 10)
$hn_input = GUICtrlCreateEdit ("", 30, 90, 215, 70)
GUICtrlSetData ($hn_input, $test_str0)
GUICtrlCreateLabel ("2.  Enter iLO static IP address:", 30, 180, 250, 20)
GUICtrlSetFont (-1, 10)
$ip_input = GUICtrlCreateEdit ("", 30, 200, 215, 70)
GUICtrlSetData ($ip_input, $test_str1)
GUICtrlCreateLabel ("3.  Enter iLO default password:", 30, 290, 200, 20)
GUICtrlSetFont (-1, 10)
$pw_input = GUICtrlCreateEdit ("", 30, 310, 215, 70)
GUICtrlSetData ($pw_input, $test_str2)
$log_label = GUICtrlCreateList ("", 300, 90, 270, 292, BitOR($WS_BORDER, $WS_VSCROLL, $WS_HSCROLL))
$generate_btn = GUICtrlCreateButton ("Generate XML File", 55, 400, 150, 30)
GUICtrlSetFont (-1, 10)
GUICtrlCreateLabel ("version 2.0", 530, 425, 70)
GUICtrlSetColor (-1, 0x0000cd)
$clrLog_btn = GUICtrlCreateButton ("Clear Log", 385, 400, 100, 30)
GUICtrlSetFont (-1, 10)

GUISetState ()

Do
   $msg = GUIGetMsg()

   Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case $msg = $generate_btn

        ; Check entry simliarity on all fields
        $hostname = StringSplit (GUICtrlRead ($hn_input), @CRLF, 3)
        $host_Count = UBound ($hostname)
        $ip_address = StringSplit (GUICtrlRead ($ip_input), @CRLF, 3)
        $ip_Count = UBound ($ip_address)
        $password = StringSplit (GUICtrlRead ($pw_input), @CRLF, 3)
        $pw_Count = UBound ($password)

        If ($host_Count = $ip_Count = $pw_Count) Then
            MsgBox (0, "", $host_Count)
            MsgBox (0, "", $ip_Count)
            MsgBox (0, "", $pw_Count)
        Else
            GUICtrlSetData ($log_label, @HOUR & ":" & @MIN & ":" & @SEC & "  Error - One or more fields doesn't match!")
            MsgBox (48, "Error", "One or more field does not match!  Please check the entries and try again.")
        EndIf

        _GUICtrlListBox_UpdateHScroll ($log_label)
        Sleep (1000)

    Case $msg = $clrLog_btn
        GUICtrlSetData ($log_label, "") ; Clear log file
   EndSelect
Until 0
Edited by dreamzboy

Share this post


Link to post
Share on other sites
JayHawkfl

Which of these three will be the most reliable or which would be the "primary" varable. You can compare the other two to the most "stable" or "reliable" one and get the results you want.

It would look like 

If $host_Count and $ip_Count = $pw_Count Then

Thanks for your time

 

Share this post


Link to post
Share on other sites
BrewManNH

 

Which of these three will be the most reliable or which would be the "primary" varable. You can compare the other two to the most "stable" or "reliable" one and get the results you want.

It would look like 

If $host_Count and $ip_Count = $pw_Count Then

That would evaluate to true if $host_count held anything other than 0 and $ip_count was equal to $pw_count, it would not check to see if $host_count was equal to any other variable in the comparison.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
dreamzboy

It didn't work.  :(

If ($host_Count And $ip_Count = $pw_Count) Then

$host_Count = 2, $ip_Count = 3, $pw_Count = 3, the script still executed.  The error is now reversed of what I had before with $pw_Count being ignored.

Share this post


Link to post
Share on other sites
JayHawkfl

It didn't work.  :(

If ($host_Count And $ip_Count = $pw_Count) Then

$host_Count = 2, $ip_Count = 3, $pw_Count = 3, the script still executed.  The error is now reversed of what I had before with $pw_Count being ignored.

I didn't have my full thoughts together, but apparently I'm off base any way.

I'll tinker with it and get back to you


Thanks for your time

 

Share this post


Link to post
Share on other sites
kylomas

dreamzboy,

Try it like this (untested).  Note how multiple vars are displayed in once instance of msgbox...

if $host_count = $ip_count and $host_count = $pw_count then
    msgbox($mb_ok,'TRUE', $ip_count & @crlf & $host_count & @crlf & $pw_count)
Else
    msgbox($mb_ok,'FALSE', $ip_count & @crlf & $host_count & @crlf & $pw_count)
endif

kylomas

edit: $MB_OK is declared in CONSTANTS.AU3

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
dreamzboy

Kylomas,

I've tried your method and it tends to work with a bit of a problem as illustrated in the screenshot below.  Notice the value $host_Count and $ip_Count didn't match.  Same result with Jos's suggestion.

yzyf.jpg

Edited by dreamzboy

Share this post


Link to post
Share on other sites
dreamzboy

I can probably live with the wrong count but unfortunately, my next line of code depends on the $host_Count.

For $i = 0 To UBound ($hostname) - 1

Share this post


Link to post
Share on other sites
AdamUL

Since you are working with 3 values you will have 3 conditions. Try this.

If $host_Count = $ip_Count And $ip_Count = $pw_Count And $host_Count = $pw_Count Then
    MsgBox($MB_OK,'TRUE', $ip_Count & @crlf & $host_Count & @crlf & $pw_Count)
Else
    MsgBox($MB_OK,'FALSE', $ip_Count & @crlf & $host_Count & @crlf & $pw_Count)
endif

Adam

Edited by AdamUL

Share this post


Link to post
Share on other sites
Melba23

dreamzboy,

I think that the comparison should be coded like this:

#include <Constants.au3>

$host_Count = 3
$ip_Count = 2
$pw_Count= 2

If ($ip_Count = $host_Count) And ($pw_Count = $host_Count) Then ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    MsgBox($MB_SYSTEMMODAL, "Result!", "All match")
Else
    MsgBox($MB_SYSTEMMODAL, "Error!", "There is a mismatch")
EndIf
You check that both the ip and pw totals match the host total - if that is the case then all of them must be the same. It certainly works when I test it - how about you? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Chimp

this should work to check if 3 or more variables have all the same value:

$host_Count = 2
$ip_Count = 2
$pw_Count = 2

If $host_Count = BitAND($ip_Count, $pw_Count) Then  ; <---- you can add up to 255 variables inside parenthesis
    MsgBox(0,"","all variables has same value")
Else
    MsgBox(0,"","all variables has NOT the same value")
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
dreamzboy

Thank you guys.  Everyone's method works but my script seems to have glitches that I need to sort it out.

Share this post


Link to post
Share on other sites
kylomas

dreamzboy,

This works for me...

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <GuiListBox.au3>
#include <array.au3>
#include <constants.au3>

Opt ("TrayIconDebug", 1)

Local $ribcl_temp = '', $xml_temp, $i
Local $hn_input, $ip_input, $pw_input
Local $host_Count, $ip_Count, $pw_Count
Local $msg, $success, $password, $hostname, $ip_address
Local $log_label, $generate_btn, $clrLog_btn
Local $test_str0 = "dl360g7-25" & @CRLF & "dl360g7-26" & @CRLF & "dl360g7-27"
Local $test_str1 = "10.112.15.22" & @CRLF & "10.112.15.23" & @CRLF & "10.112.15.24"
Local $test_str2 = "pass1" & @CRLF & "pass2" & @CRLF & "pass3"

GUICreate ("Test", 600, 450)

GUICtrlCreateLabel ("1.  Enter hostname (ex: dl360g7-25):", 30, 70, 250, 20)
GUICtrlSetFont (-1, 10)
$hn_input = GUICtrlCreateEdit ("", 30, 90, 215, 70)
GUICtrlSetData ($hn_input, $test_str0)
GUICtrlCreateLabel ("2.  Enter iLO static IP address:", 30, 180, 250, 20)
GUICtrlSetFont (-1, 10)
$ip_input = GUICtrlCreateEdit ("", 30, 200, 215, 70)
GUICtrlSetData ($ip_input, $test_str1)
GUICtrlCreateLabel ("3.  Enter iLO default password:", 30, 290, 200, 20)
GUICtrlSetFont (-1, 10)
$pw_input = GUICtrlCreateEdit ("", 30, 310, 215, 70)
GUICtrlSetData ($pw_input, $test_str2)
$log_label = GUICtrlCreateList ("", 300, 90, 270, 292, BitOR($WS_BORDER, $WS_VSCROLL, $WS_HSCROLL))
$generate_btn = GUICtrlCreateButton ("Generate XML File", 55, 400, 150, 30)
GUICtrlSetFont (-1, 10)
GUICtrlCreateLabel ("version 2.0", 530, 425, 70)
GUICtrlSetColor (-1, 0x0000cd)
$clrLog_btn = GUICtrlCreateButton ("Clear Log", 385, 400, 100, 30)
GUICtrlSetFont (-1, 10)

GUISetState ()

Do
   $msg = GUIGetMsg()

   Select
    Case $msg = $GUI_EVENT_CLOSE
        Exit
    Case $msg = $generate_btn

        ; Check entry simliarity on all fields
        $hostname = StringSplit (GUICtrlRead ($hn_input), @CRLF, 3)
        $host_Count = UBound ($hostname)
        $ip_address = StringSplit (GUICtrlRead ($ip_input), @CRLF, 3)
        $ip_Count = UBound ($ip_address)
        $password = StringSplit (GUICtrlRead ($pw_input), @CRLF, 3)
        $pw_Count = UBound ($password)
        if $host_count = $ip_count and $host_count = $pw_count then
            msgbox($mb_ok,'TRUE', $ip_count & @crlf & $host_count & @crlf & $pw_count)
        Else
            GUICtrlSetData ($log_label, @HOUR & ":" & @MIN & ":" & @SEC & "  Error - One or more fields doesn't match!")
            MsgBox (48, "Error", "One or more field does not match!  Please check the entries and try again.")
        EndIf

        _GUICtrlListBox_UpdateHScroll ($log_label)
        Sleep (1000)

    Case $msg = $clrLog_btn
        GUICtrlSetData ($log_label, "") ; Clear log file
   EndSelect
Until 0

If you want to enforce case sensitivity, check out post #12 above (PincoPanco).  This is much less cumbersome for multiple variables.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
dreamzboy

If there were to be an empty line "@CRLF" at the end of the input string, all of the checks mentioned above would failed.  :mad2:  :P

Is there a way to check for new line so that the if statement would work as intended?  I know there's a way somewhere in the forum but if you know a quick way to fix this, please let me know.

Much appreciated,

Dreamzboy

Share this post


Link to post
Share on other sites
mikell

You have to do the job using : StringStripWS($the_string, 3)

Share this post


Link to post
Share on other sites
BrewManNH

You should probably be using a ListBox instead of an Edit control to prevent the possibility of extra lines in your data.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
dreamzboy

Thanks Mikell.  I'll give that a try.

 

You should probably be using a ListBox instead of an Edit control to prevent the possibility of extra lines in your data.

 

Can the user copy/paste a list string into the box?  I'm assuming it's one by one input.  Do you have any example?

Thanks!

Share this post


Link to post
Share on other sites
dreamzboy

Actually, the white space is controlled by the if statement above.  If it detects a white space, the script shouldn't run if the condition is true.  I was overly excited that the script is working and forgot about that logic.  :dance:

Share this post


Link to post
Share on other sites
dreamzboy

Sorry guys, I just discovered that the condition is true even if all 3 inputs are zero.  This is a bug.  I do not want the script to run if there's no input.

In short, the script can be ran if all 3 inputs are the same and all 3 cannot be zero.  I'm not sure how to write the syntax or logic for it.

What I do know is that this statement does not work.

If ($host_Count = $ip_Count And $ip_Count = $pw_Count And $host_Count = $pw_Count) And (Not ($host_Count And $ip_Count And $pw_Count) = 0) Then
            Return 1;
    EndIf
Edited by dreamzboy

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  

×