Winnipeg Posted February 9, 2010 Share Posted February 9, 2010 Hi guys, I'm currently working on a script that will hopefully split and store items from a csv/txt file to a dimentional array. The csv/txt file i'm hoping to get the data from looks like this: some,1,2,3 text,4,5,6 here,7,8,9 So far my code is like this: $file = FileOpen("users.txt", 0) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop $sARRAY = stringsplit($line,",") MsgBox(0, "Line read:", $sARRAY[1]) MsgBox(0, "Line read:", $sARRAY[2]) MsgBox(0, "Line read:", $sARRAY[3]) MsgBox(0, "Line read:", $sARRAY[4]) Wend FileClose($file) Any suggestions appreciated Link to comment Share on other sites More sharing options...
Mison Posted February 9, 2010 Share Posted February 9, 2010 (edited) MsgBox(0, "Line read:", $sARRAY[1]) MsgBox(0, "Line read:", $sARRAY[2]) MsgBox(0, "Line read:", $sARRAY[3]) MsgBox(0, "Line read:", $sARRAY[4]) Would better be written as: For $i = 1 To $sARRAY[0] MsgBox(0, "Line read:", $sARRAY[$i]) Next Edited February 9, 2010 by Mison Hi ;) Link to comment Share on other sites More sharing options...
omikron48 Posted February 9, 2010 Share Posted February 9, 2010 (edited) #include <File.au3> Global $path = "users.txt" Global $lines Global $record If FileExists($path) Then $lines = _FileReadToArray($path, $lines) Local $array[$lines[0] + 1] $array[0] = $lines[0] For $i = 1 To $lines[0] $array[$i] = StringSplit($lines[$i], ",") Next $record = $array EndIf Edited February 9, 2010 by omikron48 Link to comment Share on other sites More sharing options...
Malkey Posted February 9, 2010 Share Posted February 9, 2010 Another example. #include <Array.au3> ;For display of array only. #cs Local $sSource = 'word1,word2,,word3' & @CRLF & 'word1,word2,,word3,' & @CRLF & '1,,3,4' $aArray = _CsvToArray($sSource) _ArrayDisplay($aArray) #ce ;or ;#cs Local $path = "users.txt" If FileExists($path) Then $aArray = _CsvToArray(FileRead($path)) _ArrayDisplay($aArray) EndIf ;#ce Func _CsvToArray($sSource) Local $aLines = StringSplit(StringStripWS($sSource, 2), @CRLF, 1) Local $arr[$aLines[0]][100], $iMaxCol = 0, $a, $i, $x For $i = 1 To $aLines[0] $a = StringSplit($aLines[$i], ",") If $a[0] > $iMaxCol Then $iMaxCol = $a[0] For $x = 1 To $a[0] $arr[$i - 1][$x - 1] = $a[$x] Next Next ReDim $arr[$aLines[0]][$iMaxCol] Return $arr EndFunc ;==>CsvToArray 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