Jump to content
Sign in to follow this  
souldjer777

Infinite Loop For Next when Deleting Array Elements

Recommended Posts

souldjer777

Good Morning AutoIT Gurus and Masters :sorcerer:

Slightly complicated array here... and infinite loop that I'm trying to output to a file after the values are matched up; then anything that doesn't match is deleted from the array and finally written to a file...

This guy does an infinite loop... and I'm not sure why... I tried to put in message boxes - but there are 20K records so I couldn't click through that many. LOL.

#RequireAdmin
#include <array.au3>
#include <file.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>

Global $WXYZArray01, $array01, $ProgramTitle, $sleeptime, $k01

$ProgramTitle = "Testing 1 2 3"

$sleeptime = 1000

_FileReadToArray(@ScriptDir & "\" & "testing_output_csv_unique_values.csv", $array01, "", ",")

;_ArrayDisplay ($array01, "Array01")

$aUniqueHostname = _ArrayUnique ($array01, 1)

;_ArrayDisplay ($aUniqueHostname, "UniqueHostname ")

For $i01 = Ubound($aUniqueHostname) - 1 to 0 Step - 1
    For $j01 = Ubound($array01) - 1 to 0 Step - 1
        If $array01[$j01][1] == $aUniqueHostname[$i01] and StringRegExp($array01[$j01][5], "MY_VALUE") then
            MsgBox(0, "Computer and MY_VALUE",  $aUniqueHostname[$i01] & " : " & $array01[$j01][5])
            $FileName01 = @ScriptDir & "\" & $array01[$j01][3] & "_" & $aUniqueHostname[$i01] & "_" & $array01[$j01][2] & ".csv"
             MsgBox(0, "File Name", $FileName01)
                $WXYZArray01 = $array01
                _ArrayDisplay ($WXYZArray01, "WXYZ Array")
                SplashTextOn($ProgramTitle, 'Generic - Please wait for loop to complete...', 400, 40, -1, -1, 2, "", 10)
                Sleep ($sleeptime)
                For $k01 = 0 To Ubound($WXYZArray01) - 1
                    ; MsgBox (0, "In the WXYZ loop", $WXYZArray01[$k01][1] & " : " & $aUniqueHostname[$i01])
                    If $WXYZArray01[$k01][1] <> $aUniqueHostname[$i01] Then
                        ;MsgBox (0, "In the WXYZ loop - Delete", $WXYZArray01[$k01][1] & " : " & $aUniqueHostname[$i01])
                    _ArrayDelete($WXYZArray01, $k01)
                    ;_ArrayDisplay ($WXYZArray01)
                    $k01 -= 1
                    EndIf
                    If $k01 = UBound($WXYZArray01) - 1 Then ExitLoop
                Next
                SplashOff()
                MsgBox (0, "Out of the WXYZ loop", "Out of the WXYZ loop - File Write From Array")
                _FileWriteFromArray($FileName01, $WXYZArray01, 1)
            ExitLoop
        EndIf
    Next
Next

I'm hoping someone here can tell me why at a glance... I'm a little burned out to see the answer... feeling toasty :) LOL  :idea:

Thank you everyone for your help!

:thumbsup:

 

Edited by souldjer777

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Share this post


Link to post
Share on other sites
BrewManNH

I'd guess this section might do it.

If $WXYZArray01[$k01][1] <> $aUniqueHostname[$i01] Then
    ;MsgBox (0, "In the WXYZ loop - Delete", $WXYZArray01[$k01][1] & " : " & $aUniqueHostname[$i01])
    _ArrayDelete($WXYZArray01, $k01)
    ;_ArrayDisplay ($WXYZArray01)
    $k01 -= 1 ; <<<<<<<<<<<<<<<<<<<<<<<<<<
EndIf

Every time you don't find a match, you're essentially restarting the loop, or at least making it run longer.


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
souldjer777

Thank you BrewManNH,

I'm trying to get a grasp on what's going on here... it seems stuck, like in an infinite loop still... I don't know where either...

For $i01 = Ubound($aUniqueHostname) - 1 to 0 Step - 1
    For $j01 = Ubound($array01) - 1 to 0 Step - 1
        If $array01[$j01][1] == $aUniqueHostname[$i01] and StringRegExp($array01[$j01][5], "MY_VALUE") then
            MsgBox(0, "Computer and MY_VALUE",  $aUniqueHostname[$i01] & " : " & $array01[$j01][5])
            $FileName01 = @ScriptDir & "\" & $array01[$j01][3] & "_" & $aUniqueHostname[$i01] & "_" & $array01[$j01][2] & ".csv"
             MsgBox(0, "File Name", $FileName01)
                $WXYZArray01 = $array01
                ;_ArrayDisplay ($WXYZArray01, "WXYZ Array")
                SplashTextOn($ProgramTitle, 'Generic - Please wait for loop to complete...', 400, 40, -1, -1, 2, "", 10)
                Sleep ($sleeptime)
                For $k01 = Ubound($WXYZArray01) - 1 to 0 Step - 1


                    ;MsgBox (0, "In the WXYZ loop", $WXYZArray01[$k01][1] & " : " & $aUniqueHostname[$i01])
                    If $WXYZArray01[$k01][1] <> $aUniqueHostname[$i01] Then
                        ;MsgBox (0, "In the WXYZ loop - Delete", $WXYZArray01[$k01][1] & " : " & $aUniqueHostname[$i01])
                    _ArrayDelete($WXYZArray01, $k01)
                    ;_ArrayDisplay ($WXYZArray01)
                    Else
                    EndIf
                    ;_ArrayDisplay ($WXYZArray01)
                Next
                SplashOff()
                MsgBox (0, "Out of the loop", "Out of the loop - File Write From Array")
                _FileWriteFromArray($FileName01, $WXYZArray01, 1)

            ExitLoop
        EndIf
    Next
Next

I'll keep plugging away, but thank you for your input!

Edited by souldjer777

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Share this post


Link to post
Share on other sites
souldjer777

If found the answer thanks to an answer given to me 3 years earlier by czardas - Many thanks czardas . I didn't realize the extent of your answer then, but I understand it today. Thank you.

#RequireAdmin
#include <array.au3>
#include <file.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>

Global $WXYZArray01, $array01, $ProgramTitle, $sleeptime, $k01, $aExtract, $aArray

$ProgramTitle = "Testing 1 2 3"

$sleeptime = 1000

_FileReadToArray(@ScriptDir & "\" & "testing_output_csv_unique_values.csv", $array01, "", ",")

;_ArrayDisplay ($array01, "Array01")

$aUniqueHostname = _ArrayUnique ($array01, 1)

;_ArrayDisplay ($aUniqueHostname, "UniqueHostname ")

For $i01 = Ubound($aUniqueHostname) - 1 to 0 Step - 1
    For $j01 = Ubound($array01) - 1 to 0 Step - 1
        If $array01[$j01][1] == $aUniqueHostname[$i01] and StringRegExp($array01[$j01][5], "MY_OTHER_VALUE") then
            MsgBox(0, "Computer and MY_OTHER_VALUE",  $aUniqueHostname[$i01] & " : " & $array01[$j01][5])
            $FileName01 = @ScriptDir & "\" & $array01[$j01][3] & "_" & $aUniqueHostname[$i01] & "_" & $array01[$j01][2] & ".csv"
             MsgBox(0, "File Name", $FileName01)

                SplashTextOn($ProgramTitle, 'Generic - Please wait for loop to complete...', 400, 40, -1, -1, 2, "", 10)
                Sleep ($sleeptime)

                Local $avResult = _ArrayFindAll($array01, $aUniqueHostname[$i01], 0, 0, 0, 0, 1)
                _ArrayDisplay($avResult, "$avResult")
                Local $aNewArray[UBound($avResult)][UBound($array01, 2)]

                    For $i = 0 To UBound($avResult) -1 ; Loop through the returned index numbers.
                        For $j = 0 To UBound($array01, 2) -1 ; Loop through each of the columns.
                            $aNewArray[$i][$j] = $array01 [$avResult[$i]] [$j] ; Populate the new array.
                        Next
                    Next

                SplashOff()
                MsgBox (0, "Out of the loop", "Out of the loop - File Write From Array")
                _FileWriteFromArray($FileName01, $aNewArray, 1)

            ExitLoop
        EndIf
    Next
Next

:thumbsup:  :sorcerer:czardas  :king:  :thumbsup:  ... and yes, I'm kinda ridonkulus about emoticons LOL


"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

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  

  • Similar Content

    • MrCheese
      By MrCheese
      Hi guys,
       
      Wondering, is there a better way, likely to use 'for...next' to add a letter to each range, by moving right -> along a range of columns in excel.
      I currently use this, but its clunky.
      If $run = 1 Then $range = "B6:B41" If $run = 2 Then $range = "C6:C41" If $run = 3 Then $range = "D6:D41" If $run = 4 Then $range = "E6:E41" If $run = 5 Then $range = "F6:F41" If $run = 6 Then $range = "G6:G41" If $run = 7 Then $range = "H6:H41" If $run = 8 Then $range = "I6:I41" If $run = 9 Then $range = "J6:J41" If $run = 10 Then $range = "K6:K41" If $run = 11 Then $range = "L6:L41" If $run = 12 Then $range = "M6:M41" If $run = 13 Then $range = "N6:N41" If $run = 14 Then $range = "O6:O41" If $run = 15 Then $range = "P6:P41" If $run = 16 Then $range = "Q6:Q41" If $run = 17 Then $range = "R6:R41" If $run = 18 Then $range = "S6:S41" If $run = 19 Then $range = "T6:T41" If $run = 20 Then $range = "U6:U41" If $run = 21 Then $range = "V6:V41" If $run = 22 Then $range = "W6:W41" If $run = 23 Then $range = "X6:X41" If $run = 24 Then $range = "Y6:Y41" If $run = 25 Then $range = "Z6:Z41" If $run = 26 Then $range = "AA6:AA41" If $run = 27 Then $range = "AB6:AB41" If $run = 28 Then $range = "AC6:AC41" If $run = 29 Then $range = "AD6:AD41" If $run = 30 Then $range = "AE6:AE41" If $run = 31 Then $range = "AF6:AF41" If $run = 32 Then $range = "AG6:AG41" If $run = 33 Then $range = "AH6:AH41" If $run = 34 Then $range = "AI6:AI41" If $run = 35 Then $range = "AJ6:AJ41" If $run = 36 Then $range = "AK6:AK41" If $run = 37 Then $range = "AL6:AL41" If $run = 38 Then $range = "AM6:AM41" If $run = 39 Then $range = "AN6:AN41" If $run = 40 Then $range = "AO6:AO41" If $run = 41 Then $range = "AP6:AP41" If $run = 42 Then $range = "AQ6:AQ41" If $run = 43 Then $range = "AR6:AR41" If $run = 44 Then $range = "AS6:AS41" If $run = 45 Then $range = "AT6:AT41" If $run = 46 Then $range = "AU6:AU41" If $run = 47 Then $range = "AV6:AV41" If $run = 48 Then $range = "AW6:AW41" If $run = 49 Then $range = "AX6:AX41" If $run = 50 Then $range = "AY6:AY41"  
      Normally, if it was going down the rows, i'd use this:
       
      For $i = 0 To UBound($iRowCount) - 1 $row = $i + 1 $range = "B"&$row&":B"&$row+1 Next  
      so something like this, but i don't know how to code sequential columns:
      For $i = 0 To UBound($iColCount) - 1 $col = $i + 1 $range = $col&"1:"&$col&"40" Next  
      If I don't make sense, let me know.
      Any help would be great. thanks
    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i would like to specify the array range to delete some items in my array. I don't know why the resulting array is the same.
      Here is my code:
       
      While $last_index >= $first_index _ArrayDelete($aFile_Array_Original, $aFile_Array_Original[$last_index]) $last_index-=1 WEnd  Is there any way to pass directly the range to ArrayDelete, avoiding the loop?
      Thanks in advance.
    • nacerbaaziz
      By nacerbaaziz
      hellow guys
      I am a blind young man
      I started a project to make an audio player
      with recorder
      I ended the player and i did not know how to recorder
      This player is Compatible with the blind and visually impaired and ordinary users
      Unfortunately I do not see so I could not merge images and icons
      So I hope you will be able to help me in this matter
      I will bring you the open source project and please help me to add images, icons and recorder, the project is requires easy access in order to be compatible with the screen readers, Please take this into account.
      the software is organized so the recorder is In a special  menu that Contains stop and start and pause and save.
      The same is true for player
      Please help
      And greetings to all
      easy player.zip
    • nlta1992
      By nlta1992
      I want creat GUI, have button 1 to 10, but only use loop for ... to ... step ... next.
      Thanks.
    • crushyna
      By crushyna
      Hi everyone!
      I'm developing small testing tool for new software in my company.
      Tool itself is working correctly (it follows scripted test commands one by one), but I'm trying to develop an error checking function.

      Mechanics are simple: every time software (external) shows specific error (via window class), program asks for next step. Continue, or abort test (and return to main GUI)?
      The error check function is switched on/off via Checkbox in GUI.

      Here are some code fragments:

      Checkbox:
      Global $idCheckbox1 = GUICtrlCreateCheckbox("Enable TP.net error handling", 352, 40, 249, 17) GUICtrlSetFont(-1, 12, 400, 0, "Calibri") GUICtrlSetColor(-1, 0x000000)  
      Checkbox switch:
      GUISetState(@SW_SHOW) Local $Choosen While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $idCheckbox1 If _IsChecked($idCheckbox1) Then $cSwitch1 = 1 Else $cSwitch1 = 0 EndIf Case $idAddTest (and so on...)  
      Function call:
      (alot of ElseIf commands...) ElseIf $aItem = $idOperatorIn Then WywolanieOkna() WinActivate($hFSO) ControlClick($hFSO, "", "[NAME:lblCashierNumber]") Sleep($Sleep_05) Send("0000019{Enter}19{Enter 2}") Sleep($Sleep_75) EndIf #EndRegion ### START ELSEIF ARMY ### Next If $cSwitch1 = 1 Then MsgBox( 0, "Error check!", "Error check ON!") /// just for testing purposes CheckError() Else Return EndIf EndSwitch WEnd
      Classic _IsChecked function:
      Func _IsChecked($idControlID) Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED EndFunc ;==>_IsChecked  
      And Checkerror() function itself:
      Func CheckError() Local $Answer If ControlCommand($hMsgPanel, "", "[NAME:panelMsgBox]", "IsEnabled", "") Then ;~ Sleep($Sleep_05) $Answer = MsgBox(52, "Error!", "An error occured! Continue testing?") ;~ Sleep($Sleep_05) Switch $Answer Case 6 MsgBox(0, "Error!", "Test will now continue.", 3) Send("{Enter}") WinActivate($hWnd) Return Case 7 MsgBox(64, "Error!", "Test cancelled.", 5) $ElseIfArmyOff = 1 ControlClick($hMsgPanel, "", "[NAME:cmdOK]") WinActivate($AppName) #cs MsgBox( 0, "test1", $idTSList) Local $idTSListCopy For $i = 0 to _GUICtrlListView_GetItemCount($idTSList) - 1\ #ce $idTSListCopy = _GUICtrlListView_GetItemTextString($idTestCaseList, $LoopCount) ExitLoop EndSwitch EndIf Return 1 EndFunc ;==>CheckError
      Problem is: i can't make this function (CheckError() ) return to main gui state, that means: break the If/ElseIf loop.
      The switch is working correctly (MsgBox on error shows only when it's activated).
      Checkerror() function works correctly - recognizes the error, stops script until decision is made. But it doesn't terminate If/ElseIf loop.

      Any help appreciated!
×