Jump to content

How to compare a value to an array of words


kor
 Share

Recommended Posts

Currently I am doing a crazy IF Or, Or, Or statement to compare a bunch of inputs to a bunch of different values.

If GUICtrlRead($aInput[0][0]) = "First Name" GUICtrlRead($aInput[0][0]) = "" Or GUICtrlRead($aInput[1][0]) = "Last Name" Or GUICtrlRead($aInput[1][0]) = "" _GUICtrlRead($aInput[2][0]) = "Staff ID" Or _
            GUICtrlRead($aInput[3][0]) = "Badge ID" Or GUICtrlRead($aInput[4][0]) = "Location" Or GUICtrlRead($aInput[5][0]) = "Job Title" Or _
            GUICtrlRead($aInput[6][0]) = "Classification" Or GUICtrlRead($aInput[7][0]) = "Extension" Then

Basically I'd like to simplify this a bit. I'd like to compare each of the $aInput values to an array of keywords and return if there is a match or not.

My keyword array would be static containing specific words.

How might I go about doing this?

Link to comment
Share on other sites

It's a little difficult with the info provided, but here is a generic example.

It assumes your key word array is the same size as your input array.

For $i = 0 To UBound($aInput) - 1
If GUICtrlRead($aInput[$i][0] == $aKeyword[$i] Then
  ;stuff
EndIf
Next

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

It's not entirely clear how exact you want the match to be - case sensitive, exact word etc... Anyway with two arrays you should use nested For loops.

#include <Array.au3>

Local $aKeywords[5] = ["one","two","three","four","five"]
Local $aInput[5] = ["two","beer","five cats","four","done"]

For $i = 0 To 4
    For $j = 0 To 4
        If $aInput[$i] = $aKeywords[$j] Then _
        MsgBox(0, "Match Found for", $aKeywords[$j])

        If StringInStr($aInput[$i], $aKeywords[$j]) Then _
        MsgBox(0, $aInput[$i] & " - contains the string", $aKeywords[$j])
    Next
Next
Edited by czardas
Link to comment
Share on other sites

Sorry, the $aInputs are just GUI input boxes

$aInput[0][0] = GUICtrlCreateInput("First Name", 25, 75, 140, 20)
    $aInput[1][0] = GUICtrlCreateInput("Last Name", 188, 75, 140, 20)
    $aInput[2][0] = GUICtrlCreateInput("Staff ID", 25, 101, 140, 20)
    $aInput[3][0] = GUICtrlCreateInput("Badge ID", 188, 101, 140, 20)
    $aInput[4][0] = GUICtrlCreateCombo("Location", 25, 126, 140, 18, $CBS_DROPDOWNLIST)

The number of inputs would not be the same array size as the keywords array.

I'd be looking for exact match.

Edited by kor
Link to comment
Share on other sites

The gui inputs have default vaules IE "first name", "last name", etc.

When I do a read of whats in the input I want to throw an error if the value after reading the input is the same as the default words I used.

I don't want the script to be allowed to continue if the person left the "first name" input with the "first name" text inside of it. I can't check for it being "blank" because its not techncially blank, it has a value.. I just want to know if the value thats in it would be different than the example text I used to descibe the input.

Link to comment
Share on other sites

The code was just an example. You have to modify it to suit your needs. Case sensitive and exact match.

For $i = 0 To UBound($aInput) -1
    For $j = 0 To UBound($aKeywords) -1
        If GUICtrlRead($aInput[$i][0]) == $aKeywords[$j] Then
        MsgBox(0, "Exact match Found for", $aKeywords[$j])
    Next
Next
Edited by czardas
Link to comment
Share on other sites

The gui inputs have default vaules IE "first name", "last name", etc.

When I do a read of whats in the input I want to throw an error if the value after reading the input is the same as the default words I used.

I don't want the script to be allowed to continue if the person left the "first name" input with the "first name" text inside of it. I can't check for it being "blank" because its not techncially blank, it has a value.. I just want to know if the value thats in it would be different than the example text I used to descibe the input.

In this case, your array of input values should match the amount of input controls.

In fact, you should probably use your keyword array to populate them to start with.

ie

$aKeywords[4] = ["First Name", "Last Name", "Staff ID", "Badge ID"]
$aInput[0][0] = GUICtrlCreateInput($aKeywords[0], 25, 75, 140, 20)
$aInput[1][0] = GUICtrlCreateInput($aKeywords[1], 188, 75, 140, 20)
$aInput[2][0] = GUICtrlCreateInput($aKeywords[2], 25, 101, 140, 20)
$aInput[3][0] = GUICtrlCreateInput($aKeywords[3], 188, 101, 140, 20)
For $i = 0 To UBound($aInput) - 1
If $i = 2 Then ContinueLoop; or the field you want to omit
If GUICtrlRead($aInput[$i][0] == $aKeywords[$i] Then
  ;return error, or something
EndIf
Next

EDIT: include omission of control

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

In fact, you should probably use your keyword array to populate them to start with.

Can't. The names of the inputs changes. Sometimes $aInput[1][0] could equal "First Name" other times it could equal "Username"

It changes depending on some options the user can set somewhere else in the script.

Link to comment
Share on other sites

Either reorder your array of controls or skip the controls you don't want to test. Make the choice, which is easier?

Edit

Hmm, I didn't read your last post properly. If the content of various controls changes like that then perhaps the design is not easy to understand for you or the user. If you wish to reuse the same input controls for different input criteria, then you need to keep track of type of the input requested from the user (first name or username). This means storing more variables and added complication.

Edited by czardas
Link to comment
Share on other sites

Can't. The names of the inputs changes. Sometimes $aInput[1][0] could equal "First Name" other times it could equal "Username"

It changes depending on some options the user can set somewhere else in the script.

That's fine, but to me the logic should remain the same.

is there at least a set number of controls that need to be tested against not being filled in?

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Either reorder your array of controls or skip the controls you don't want to test. Make the choice, which is easier?

I'll skip controls using the ContinueLoop, thanks.

That's fine, but to me the logic should remain the same.

is there at least a set number of controls that need to be tested against not being filled in?

No. For one option im testing if only 3 controls.. in another im testing for 6, etc.

I have a working model using

For $i = 0 To UBound($aInput) - 1
                For $n = 0 To UBound($aKeywords) - 1
                    If GUICtrlRead($aInput[$i][0]) = $aKeywords[$n] Then
                        ConsoleWrite("invalid")
                        ExitLoop
                    EndIf
                Next
            Next

The problem is that I am only exiting the first loop. How do I exit both loops if a match is found?

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...