Grantson Posted December 13, 2012 Share Posted December 13, 2012 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 More sharing options...
FireFox Posted December 13, 2012 Share Posted December 13, 2012 (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 December 13, 2012 by FireFox Link to comment Share on other sites More sharing options...
Grantson Posted December 13, 2012 Author Share Posted December 13, 2012 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 Link to comment Share on other sites More sharing options...
kylomas Posted December 13, 2012 Share Posted December 13, 2012 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 Link to comment Share on other sites More sharing options...
kylomas Posted December 13, 2012 Share Posted December 13, 2012 (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]) Nextkylomasedit: code is shit Edited December 13, 2012 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 More sharing options...
kylomas Posted December 13, 2012 Share Posted December 13, 2012 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 More sharing options...
FireFox Posted December 13, 2012 Share Posted December 13, 2012 @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. Link to comment Share on other sites More sharing options...
kylomas Posted December 13, 2012 Share Posted December 13, 2012 @FF - I'm talking about my code, not yours 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 More sharing options...
FireFox Posted December 13, 2012 Share Posted December 13, 2012 @FF - I'm talking about my code, not yours I was talking to Grantson Link to comment Share on other sites More sharing options...
kylomas Posted December 13, 2012 Share Posted December 13, 2012 (edited) grantson, Here is a function I wrote for debuging. It turns a delimited string into a 2D array. expandcollapse popup#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 December 13, 2012 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 More sharing options...
czardas Posted December 13, 2012 Share Posted December 13, 2012 (edited) @Grantson: You might want to check out:Search Example scripts forum for other examples. Edited December 13, 2012 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now