Jump to content

grief with nested for loops and multi-dimensional arrays


Grantson
 Share

Recommended Posts

Hi guys

Im working on a script to parse a CSV file (delimited with ;'s)

I read the file to an array then i want to use a nested loop to step through the array use stringsplit() to create a second array

thuse far my attemts either return a huge empty array or give an Array variable has incorrect number of subscripts or subscript dimension range exceeded.: error

Could someone cast an eye over my code and tell me what im missing?

Global $oldcomp_Multi_Array[$oldcomp_Array[0]][16]

$j=29
For $j = 29 To UBound($oldcomp_Array)-1
      $tmp=stringSplit($oldcomp_Array[$j],";")

      for $t = 1 to UBound($tmp)-1
           $oldcomp_Multi_Array[$j][$t] = $tmp[$t]
           Next

     Next

_ArrayDisplay($oldcomp_Multi_Array)

Thanks

Grant

Link to comment
Share on other sites

Hi,

Maybe this (not tested) :

#include <File.au3>

Global $aFile

_FileReadToArray("file.csv", $aFile)
If @error Then Exit -1

Global $aData[$aFile[0]], $atmp

For $iLine = 1 To $aFile[0]
$aData[$iLine - 1] = StringSplit($aFile[$iLine], ";", 2)
Next

$atmp = $aData[0]

ConsoleWrite($atmp[0] & @CRLF)

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

grantson,

Slightl modified code from FireFox. This will display an array for each row of the file. (Not tested)

#include <File.au3>
#include <array.au3>

Global $aFile

_FileReadToArray("file.csv", $aFile)
If @error Then Exit -1

Global $aData[$aFile[0]], $atmp

For $iLine = 1 To $aFile[0]
$aData[$iLine - 1] = StringSplit($aFile[$iLine], ";", 2)
_arraydisplay($adata[$iLine])
Next

kylomas

edit: code is shit

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

grantson,

Is this what you are looking for (tested)

#include <File.au3>
#include <array.au3>

local $aFile

_FileReadToArray(@scriptdir & "csvtest.txt", $aFile)
If @error Then Exit -1

local $aData

For $iLine = 1 To $aFile[0]
    $aData = StringSplit($aFile[$iLine], ";", 2)
    _arraydisplay($adata)
Next

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

grantson,

Here is a function I wrote for debuging. It turns a delimited string into a 2D array.

#include <File.au3>
#include <array.au3>

local $sFile = fileread(@scriptdir & 'csvtest.txt')

local $a_2d = _DBG_StringSplit2D($sFile,';')

_arraydisplay($a_2d)

; #FUNCTION# ======================================================================================
; Name ................:    _DBG_StringSplit2D($str,$delimiter)
; Description .........:    create 2d array from EOL delimited string
; Syntax ..............:    _DBG_StringSplit2D($str, $del)
; Parameters ..........:    $str         - string to split
;                           $delimiter     - the delimter for columns
; Return values .......:
; Author ..............:     kylomas
; Modified.............:
; Remarks .............:
; Related .............:
; Link ................:
; Example .............:
; =================================================================================================

func _DBG_StringSplit2d(byref $str,$delimiter)

    local $a1 = stringsplit($str,@lf,2), $a2

    local $rows = ubound($a1) - 1, $cols = 0

    ; determine max number of columns by splitting each row and keeping highest ubound value

    for $i = 0 to ubound($a1) - 1
        $a2 = stringsplit($a1[$i],$delimiter,1)
        if ubound($a2) > $cols then $cols = ubound($a2)
    next

    ; define and populate array

    local $o[$rows][$cols]

    for $i = 0 to $rows - 1
        $a2 = stringsplit($a1[$i],$delimiter,1)
        for $j = 0 to ubound($a2) - 1
            if $j = 0 then $a2[$j] = $a2[$j] - 1    ; adjust column count for 0 offset
            $o[$i][$j] = $a2[$j]
        Next
    next

    return $o

endfunc

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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