Jump to content

Infinite Loop For Next when Deleting Array Elements


Go to solution Solved by souldjer777,

Recommended Posts

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

Link to post
Share on other sites

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

Link to post
Share on other sites

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

Link to post
Share on other sites
  • Solution

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

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By roeselpi
      hello again,
      it has been a long time since i have been here and a long time since i last used autoit. ever so often when the time allows me to, then i follow up on an idea that i had a long time ago. i have done all the work on paper but now it is up to writing it in autoit and i keep stumbling over many little issues here and there. sometimes after a few days i will try again and get a step further but sometimes it just will not help no matter how long i try and think about a solution. for most of you it will be the basics but for me it is not all that easy, but at least i give it a try.
      right, down to business:
      here is my code:
      #include <MsgBoxConstants.au3> #include <StringConstants.au3> #include <Array.au3> #include <String.au3> ; ; PART 1: define replacements and check with msgbox ; Global $y, $z $y = "Yes" $z = "No" MsgBox(0,"replacements", $y & @CRLF & $z) ;the replacements in a message box ; ; PART 2: set the texts and check via console and msgbox ; Global $my1string = "abab" ;the first specified text MsgBox(0,"my1string", $my1string) ;the message box to output the first specified text Global $my2string = "icic" ;the second specified text MsgBox(0,"my2string", $my2string) ;the message box to output the second specified text ; ; PART 3: transform the strings to individual arrays ; $my1array = StringSplit($my1string, "") $my1array[0] = "" _ArrayDelete($my1array, 0) _ArrayDisplay($my1array, "my1array") ;the display of the first specified array $my2array = StringSplit($my2string, "") $my2array[0] = "" _ArrayDelete($my2array, 0) _ArrayDisplay($my2array, "my2array") ;the display of the first specified array ; ; PART 4: create an empty array for filling ; Global $OutputArray[4] $OutputArray[0] = "" _ArrayDisplay($OutputArray, "OutputArray") ;the display of the first specified array ; ; PART 5: compare & fill empty OutputArray with data after evaluation ; Global $i, $j, $k For $i = 0 to UBound($my1array) -1 For $j = 0 to UBound($my2array) -1 For $k = 0 to UBound($OutputArray) -1 If $my1array[$i] = "a" And $my2array[$j] = "i" Then $OutputArray[$k] = $y Else $OutputArray[$k] = $z EndIf Next Next Next _ArrayDisplay($OutputArray, "OutputArray") ;the display of the Newly filled Array In "Part 2" i make a string that is converted to an array in "Part 3" ... Now, I know that "a" and "i" are always in the exact same spot in both arrays and so i wanted to compare this and make a further array to document my findings by saying "yes" or "no" ... however my new array keeps saying just "no" allthough i can clearly see and know that it should say:
      yes no yes no my guess is that there is something wrong within my for-loops and that the counting is somehow "off" i guess that when the first for-loop is finished it reaches the second whilst the second for-loop is checking the first which would explain why it always says "no" instead of seeing the obvious.
      so my question would be: what is wrong with my for-loop? or where am i making an error that ultimately gives me the wrong results?
      help is much appreciated.
      kind regards
      roeselpi
       
       
      PS: sorry for my not so great english spelling ... stupid german sitting here trying out intermediate english skills.
    • By plankton
      Hi I want it to search for a specific color in certain area then excute the loop which presses ESC button and stop the loop when the color no longer present in that specific area.  Here is the example of my code. But it doesnt stop when color is disappeared.
       
      Pixelsearch(511, 455, 678, 501, 0xFFFFFF)
      If Not  @Error Then
      While 1
      Sleep(1000)
      Send("{ESC}")
      Pixelsearch(511, 455, 678, 501, 0xFFFFFF)
      If @Error Then Exitloop
       
    • By Deye
      just needed this post to get a clue of how well will this do as an alternative to searching the the array @deleting empty rows 1D & 2D speed wise
      Note: for the 1D the method doesn't work if the chosen delimiter exists in any of the array elements ..(not about the "|" one) 
      So Here is an all around fast combo that includes @Inpho's _ArrayFindEmptyRows()  that proved to be a very fast way for removing 2D empty rows 
       
    • 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
    • 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.
×
×
  • Create New...