Jump to content

Array within an array loop


Recommended Posts

Hello everyone,

I am trying to create a program that writes custom script to a text file, and then a third party program runs it.  There are 2 arrays that contain data.  I need the second array to loop entirely through printing all of it's information for each slot in the first array, like the following:

 

Array1 Info [1]

        Array2 Info [1]

        Array2 Info [2]

        Array2 Info [3]

Array1 Info [2]

        Array2 Info [1]

        Array2 Info [2]

        Array2 Info [3]

etc...

I have been messing around with code, this is what I'm using right now (cut out just the useful parts):

#include <file.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

$sFileName = "C:\Parameters.txt" ;The file that I am writing all this script to

$Data = _GUICtrlListBox_GetSelItemsText($List1) ;Gets data from a list that I have, which works fine
$Upload = GUICtrlRead($UploadList) ;This is just an edit box that is being read raw

$UploadArray = _CSVSplit($Upload,@TAB) ;Uploading the $Upload information into an array variable using a slightly altered version of the CSVSplit UDF
_ArrayDisplay($UploadArray) ;This displays the $UploadArray variable, and it displays CORRECTLY, so I know this works this far

For $iI = 1 To $Data[0]
    $sItems = ""
    $sItems &= @CRLF & $Data[$iI]
    FileWrite($hFilehandle, @CRLF & 'cd "/FTP/Files/' & StringStripWS($sItems,$STR_STRIPLEADING) & '/"') ;This part works great, it loops through fine
    For $iT = 1 To $UploadArray[0]
        $ArrayItems = ""
        $ArrayItems &= @CRLF & $UploadArray[$iT]
        FileWrite($hFilehandle, @CRLF & "put " & $ArrayItems) ;This part does NOT work great...it just puts nothing every time, regardless of info in array
    Next
Next

I made the code for the loop within the loop exactly the same as the first part of the loop, which works well and loops through everything in the array.  But it just doesn't do anything when it gets to the loop within the loop, even though I verified that the second array is getting filled correctly (at least to my knowledge!).

Here is the lightly modified code for the _CSVSplit function (I literally just changed the $sDelim to @CRLF).  Although I don't think this is the problem since it seems to be populating correctly, I am including just for the sake of completeness and in case anyone wanted to reference this part.

Func _CSVSplit($string, $sDelim = @CRLF) ; Parses csv string input and returns a one or two dimensional array
    If Not IsString($string) Or $string = "" Then Return SetError(1, 0, 0) ; Invalid string
    If Not IsString($sDelim) Or $sDelim = "" Then Return SetError(2, 0, 0) ; Invalid string

    $string = StringRegExpReplace($string, "[\r\n]+\z", "") ; [Line Added] Remove training breaks
    Local $iOverride = 63743, $asDelim[3] ; $asDelim => replacements for comma, new line and double quote
    For $i = 0 To 2
        $asDelim[$i] = __GetSubstitute($string, $iOverride) ; Choose a suitable substitution character
        If @error Then Return SetError(3, 0, 0) ; String contains too many unsuitable characters
    Next
    $iOverride = 0

    Local $aArray = StringRegExp($string, '\A[^"]+|("+[^"]+)|"+\z', 3) ; Split string using double quotes delim - largest match
    $string = ""

    Local $iBound = UBound($aArray)
    For $i = 0 To $iBound -1
        $iOverride += StringInStr($aArray[$i], '"', 0, -1) ; Increment by the number of adjacent double quotes per element
        If Mod ($iOverride +2, 2) = 0 Then ; Acts as an on/off switch
            $aArray[$i] = StringReplace($aArray[$i], $sDelim, $asDelim[0]) ; Replace comma delimeters
            $aArray[$i] = StringRegExpReplace($aArray[$i], "(\r\n)|[\r\n]", $asDelim[1]) ; Replace new line delimeters
        EndIf
        $aArray[$i] = StringReplace($aArray[$i], '""', $asDelim[2]) ; Replace double quote pairs
        $aArray[$i] = StringReplace($aArray[$i], '"', '') ; Delete enclosing double quotes - not paired
        $aArray[$i] = StringReplace($aArray[$i], $asDelim[2], '"') ; Reintroduce double quote pairs as single characters
        $string &= $aArray[$i] ; Rebuild the string, which includes two different delimiters
    Next
    $iOverride = 0

    $aArray = StringSplit($string, $asDelim[1], 2) ; Split to get rows
    $iBound = UBound($aArray)
    Local $aCSV[$iBound][2], $aTemp
    For $i = 0 To $iBound -1
        $aTemp = StringSplit($aArray[$i], $asDelim[0]) ; Split to get row items
        If Not @error Then
            If $aTemp[0] > $iOverride Then
                $iOverride = $aTemp[0]
                ReDim $aCSV[$iBound][$iOverride] ; Add columns to accomodate more items
            EndIf
        EndIf
        For $j = 1 To $aTemp[0]
            If StringLen($aTemp[$j]) Then
                If Not StringRegExp($aTemp[$j], '[^"]') Then ; Field only contains double quotes
                    $aTemp[$j] = StringTrimLeft($aTemp[$j], 1) ; Delete enclosing double quote single char
                EndIf
                $aCSV[$i][$j -1] = $aTemp[$j] ; Populate each row
            EndIf
        Next
    Next

    If $iOverride > 1 Then
        Return $aCSV ; Multiple Columns
    Else
        For $i = 0 To $iBound -1
            If StringLen($aArray[$i]) And (Not StringRegExp($aArray[$i], '[^"]')) Then ; Only contains double quotes
                $aArray[$i] = StringTrimLeft($aArray[$i], 1) ; Delete enclosing double quote single char
            EndIf
        Next
        Return $aArray ; Single column
    EndIf
 EndFunc ;==> _CSVSplit

 Func __GetSubstitute($string, ByRef $iCountdown)
    If $iCountdown < 57344 Then Return SetError(1, 0, "") ; Out of options
    Local $sTestChar
    For $i = $iCountdown To 57344 Step -1
        $sTestChar = ChrW($i)
        $iCountdown -= 1
        If Not StringInStr($string, $sTestChar) Then
            Return $sTestChar
        EndIf
    Next
    Return SetError(1, 0, "") ; Out of options
EndFunc ;==> __GetSubstitute

Does anyone have any ideas on why this seems to be happening?  Is it something super easy that I've just overlooked or don't know about??  Is it even possible to nest for loops like this?

Thank you in advance folks!!

Link to comment
Share on other sites

Hey Subz,

Thank you for the quick reply!  As I am still fairly new to this whole array thing, so I was unaware of differences such as 1d and 2d.  This is a snapshot of the array display once I loaded some data into it:

image.png.0dcf13e65c58a857cbe867ec2ab5d6fa.png

Is there a way I can easily tell?  I presume that it's 1d because it has one column only right...?

Link to comment
Share on other sites

It is at the bottom that you know if it is 1d or 2d.  So you got [4], that tells you it is 1D.  You could have the exact same display with a 2D array but the bottom would say something like [4][1].

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