Jump to content

Broken Nested "If/Then" in While Loop


4b0082
 Share

Go to solution Solved by Melba23,

Recommended Posts

I'm not really sure what's causing this script to break; it seems to be working perfectly and then after running for some time the $a variable and my first If/Then statement quit functioning appropriately.

Snippet of script (nested in another while loop to keep it cycling).

$a = 0
$b = 0
$c = 10

While $b <> UBound($Items)
   While $b <> $c
      If $Items[$b][1] < $tArray[$a][1] Then
         If _ArrayFindAll($dupes, $Items[$b][1]) = -1 Then
            _ArrayAdd($dupes, $Items[$b][1])
            $Open = MsgBox(4, "Item Found", "Item: " & $Items[$b][2] & @LF & "Price: $" & $Items[$b][1])
            If $Open = 6 Then
               $iPage = $Items[$b][0]
               ShellExecute($iPage)
            EndIf
         EndIf
      EndIf
      $b += 1
   WEnd
   $c += 10
   $a += 1
WEnd

For some reason, my statement "If $Items[$b][1] < $tArray[$a][1] Then" is processing "If 24.95 < 4 Then" as a true value. $a SHOULD be pulling from the array as $tArray[3][1] which is a value of 20, but it's instead processing as $tArray[2][1], which is the value of 4.

$Items is an array of 40 (0-39), $tArray is an array of 4 (0-3).

Any idea what's causing this loop to break?

Edited by 4b0082
Link to comment
Share on other sites

Are all your values numeric?  Strings won't evaluate properly.  You can cast them to numbers to be sure.  Also, the whole code would be more helpful.  Finally, I would suggest some debugging to measure all the values along the way and check for errors.

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

All of my values are numeric, and I tried to troubleshoot but I still don't really know what's causing the issue. $a += 1 is definitely working, but the script is breaking before reaching that point and my If/Then is still processing "If 20 < 4 Then" as a true statement. I was kind of hoping to avoid posting my whole script, though. Is there more specific information I can provide to avoid posting the whole thing?

Do you think rewriting this part of the code in a different way might help?

Link to comment
Share on other sites

  • Moderators
  • Solution

4b0082,

Although the values appear numeric, they may well be stored as strings within the array depending on how you filled it. Use VarGetType to check - and force them to number format with Number if necessary. :)

M23

Edited by Melba23
Fixed BB tags

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Oh, apparently they were being processed as strings. Would changing this line be enough to have them processed numerically every time?

Edit: Seems like it's working so far, just have to let it run for a while to be completely sure! Thanks guys!

If Number($Items[$b][1]) < Number($tArray[$a][1]) Then
Edited by 4b0082
Link to comment
Share on other sites

  • Moderators

4b0082,

That will do the trick. :thumbsup:

It is a common problem and as soon as you find comparisons which do not appear to work correctly it should always be your first suspicion. Why does it happen? Because AutoIt variables are not typed and occasionally do not store values in the form you expect - for example anything read from an ini file is automatically stored as a string. Should it happen? Jon made a design decision all those years ago not to use typed variables for ease of use - too late to go back now and so the user has to take care in certain situations, with comparisons being the most frequent problem area. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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