Jump to content

IF statement processing all conditions


Recommended Posts

I'm having a brainfart I think, made a simple mistake but I can't figure it out. I've got part of a script where I have a conditional statement inside of a For loop, and while the program *works* I end up getting MsgBox for each of the Else fallbacks. Now, ideally this wouldn't be an issue since there should only be 1 value to match against in the For loop, but in testing we may end up having multiple values. So here is basically what I have:

For $r = 1 to Ubound($data) -1
If $data[$r][6] == $var2 Then
If FileExists ($xmlFile) Then
     ; do some stuff
Else
     MsgBox (4096, "Error", "The XML file was not found")
EndIf
Else
MsgBox (4096, "Error", "values do not match")
EndIf
Next

So basically, if there are multiple values to match against, I end up getting x amount of "values do not match" MsgBoxes, and also the match condition processes as well.

What did I mess up?

Edited by Tripredacus
Link to comment
Share on other sites

Not sure if this is a typo in the post or in your code, but $ is not a valid index. Did you intend to use $r?

If $data[$r][6] == $var2 Then

It isn't a typo. The XML file can have multiple (duplicate) objects in it. So it is only the 6th child item in an object that I need. But I do not know which object it is in, so I need to read through all of them to find the correct one. The value of $var2 will never be the same between different objects, so I can use that as a compare.

Either way, that part of the code DOES work fine, its just that the mismatch condition (show error MsgBox) gets called on all the objects where the 6th child object does not match.

Link to comment
Share on other sites

I understand what you are trying to do by looping through the data, the part I was referring to is that in your original post you have $data[$][6] instead of $data[$r][6]. This is a syntax error.

The logic is sound. I think I just misunderstood your question. I think you basically want one of two results: a message of that the item was found or a message that none of the rows in the data array matched. In that case you need to handle the 'no match' case outside of the for loop:

Dim $bFound = False

For $r = 1 to Ubound($data) -1
  If $data[$][6] == $var2 Then
    If FileExists ($xmlFile) Then
      ; do some stuff
      MsgBox(4096, "Found", "Found element")
      $bFound = True
      ExitLoop
    Else
      MsgBox (4096, "Error", "The XML file was not found")
    EndIf
 EndIf
Next

If Not $bFound Then MsgBox (4096, "Error", "values do not match")
Edited by zorphnog
Link to comment
Share on other sites

Share Moar Code.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Oh yes, sorry it was a typo.

I will definately try that out, but first I ran into another problem! Testing this code on a result where the value of $data is only in 1 dimension, I get a subscript error. :

If there's only one dimension in the array, this code will definitely give you that error because it's written to assume that the array is 2 dimensions and that it has at least 7 elements in the second dimension. If you want it to work with arrays of varying sizes, you'll need to account for that in your code.

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

Link to comment
Share on other sites

Testing this code on a result where the value of $data is only in 1 dimension, I get a subscript error. :

Try:

Dim $bFound = False

Switch UBound($data, 0)
    Case 1:
        If $data[6] == $var2 Then
            If FileExists ($xmlFile) Then
                ; do some stuff
                MsgBox(4096, "Found", "Found element")
                $bFound = True
            Else
                MsgBox (4096, "Error", "The XML file was not found")
            EndIf
        EndIf
    Case 2:
        For $r = 1 to Ubound($data) - 1
            If $data[$r][6] == $var2 Then
                If FileExists ($xmlFile) Then
                    ; do some stuff
                    MsgBox(4096, "Found", "Found element")
                    $bFound = True
                    ExitLoop
                Else
                    MsgBox (4096, "Error", "The XML file was not found")
                EndIf
            EndIf
        Next
EndSwitch

If Not $bFound Then MsgBox (4096, "Error", "values do not match")
Edited by zorphnog
Link to comment
Share on other sites

Strangely enough, with that code, I wasn't getting a match. So I did some MsgBox tests and found that I was getting an array size of 2 as the Ubound on data. I'm not certain how this ended up being the case (no pun intended), so I made 2 changes.

Switch Ubound($data, 0) -1
        Case 1
            If $data[$r][6] == $var2 Then
                BindData()
            EndIf
        Case 2 To 10
            For $r = 1 to Ubound($data) -1
                If $data[$r][6] == $var2 Then
                    BindData()
                EndIf
            Next
        Case 0
            MsgBox (4096, "Debug", "Case value is 0")
        Case Else
            MsgBox (4096, "Debug", "More than 10 allocations")
    EndSwitch

So the first change was to add -1 to the Ubound in the Switch statement. The second was to use the [$r] in the Case 1 compare.

PS: AutoIT doesn't like colons after the Case numbers. :D

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...