Sign in to follow this  
Followers 0
Grantson

grief with nested for loops and multi-dimensional arrays

11 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Thanks Firefox

The console write gave me the 1st line of the CSV file when i changed it to _arrayDisplay($atmp) i again got a blank array

Share this post


Link to post
Share on other sites

grantson,

$old_comp is a 2D array. You are running stringsplit on 1D.

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

@Grantson

The point is, that you can not directly display an array in an array; you must store it in a variable and it's the same thing for objects inside arrays.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

@FF - I'm talking about my code, not yours :bye:


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

Share this post


Link to post
Share on other sites

@FF - I'm talking about my code, not yours :bye:

I was talking to Grantson :bye:

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

@Grantson: You might want to check out:

Search Example scripts forum for other examples.

Edited by czardas

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