Sign in to follow this  
Followers 0
gcue

lost in a For Loop Nest

7 posts in this topic

#1 ·  Posted (edited)

I am trying to merge data between several CSV files.

I have an array of column header names as selected by the user (Name, Team, RBI, etc)

I'm trying to go through each of the CSV files to collect the data when a column in each of the sheets matches with the column(s) the user selected

Please see comments for more information.

Hope my explanation makes sense

;Creates Columns Array from Users Selection
    $columns_selected = _GUICtrlListView_GetItemCount($selected_columns_listview)

    Local $results_array[1][$columns_selected]
    
    For $x = 0 To $columns_selected - 1
        $selected_item_name = _GUICtrlListView_GetItemText($selected_columns_listview, $x, 0)

        $results_array[0][$x] = $selected_item_name
    Next


        ;Merge Data
    $sheets_array = IniReadSection($INI, "SHEETS") ;has the names of the sheets

    For $w = 1 To $sheets_array - 1
        $file = @ScriptDir & "\" & $sheets_array[$w][0] & ".csv"

        $array = CreateArray($file) ;creates an array from the CSV file passed through it

        $rows = UBound($array) - 1
        $columns = UBound($array, 2)

        For $x = 0 To $columns - 1
            For $y = 0 To UBound($results_array, 2) - 1
                If $array[0][$x] = $results_array[0][$y] Then
                    $current_row = 1

                    For $z = 1 To $rows
                        $results_array[$current_row][$y] = $array[$z][$x]

                        $current_row += 1
                    Next
                EndIf
            Next
        Next
    Next



        ;Output
    $rows = UBound($results_array) - 1
    $columns = UBound($results_array, 2) - 1

    For $i = 0 To $rows
        $sLine = ""
        For $j = 0 To $columns
            $sLine &= $results_array[$i][$j] & ","
        Next
        FileWriteLine($output_file, StringTrimRight($sLine, 1))
    Next

    ShellExecute($output_file)
Edited by gcue

Share this post


Link to post
Share on other sites



IniReadSection() returns a 2D array, so your iteration of the loop is wrong:

; wrong:  For $w = 1 To $sheets_array - 1

For $w = 1 To $sheets_array[0][0]

; -- or --

For $w = 1 To Ubound($sheets_array) - 1

:unsure:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

ah nice catch psalty.. still getting an error here though:

$results_array[$current_row][$y] = $array[$z][$x]

"Array variable has incorrect number of subscripts or subscript dimension range exceeded."

Share this post


Link to post
Share on other sites

hmm but doesnt it do the inner-most loop a different number of times than the outter-most loop?

Share this post


Link to post
Share on other sites

Run it like this to get an idea where it went wrong:

; ...
                ConsoleWrite("Debug: $results_array[" & Ubound($results_array) & "][" & Ubound($results_array, 2) & "]" & _
                    "; $array[" & Ubound($array) & "][" & Ubound($array, 2) & "]" & @LF)
                For $z = 1 To $rows
                    Consolewrite(@TAB & "$results_array[" & $current_row & "][" & $y & "] = $array[" & $z & "][" & $x & "]" & @LF)
                    $results_array[$current_row][$y] = $array[$z][$x]
                    $current_row += 1
                Next

:unsure:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

figured it out!

i needed to redim the values for rows and columns :unsure:

$sheets_array = IniReadSection($INI, "SHEETS")

    For $w = 1 To UBound($sheets_array) - 1
        $file = @ScriptDir & "\" & $sheets_array[$w][0] & ".csv"

        $array = CreateArray($file)

        $rows = UBound($array) - 1
        $columns = UBound($array, 2)

        ReDim $results_array[$rows + 1][$columns]

        For $x = 0 To $columns - 1
            For $y = 0 To UBound($results_array, 2) - 1
                If $array[0][$x] = $results_array[0][$y] Then
                    $current_row = 1

                    For $z = 1 To $rows
                        ConsoleWrite($z & @CRLF)
                        $results_array[$current_row][$y] = $array[$z][$x]

                        $current_row += 1
                    Next
                EndIf
            Next
        Next
    Next

thanks for your help =)

Edited by gcue

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  
Followers 0