lafafmentvotre Posted May 18, 2010 Share Posted May 18, 2010 (edited) Hi everybody and sorry for my englishI need to read a txt files who contains variable informations like this :computer1,108838computer2,108836computer3,108867computer4,108825and use the two arguments for each line with a for..to...next (computer and number)But in my array, i obtain this :Who can help me to find different value for each linesThanks for help Edited May 18, 2010 by lafafmentvotre Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 18, 2010 Share Posted May 18, 2010 There have been several CSV UDFs posted. Do a little searching. At least one of them was posted by SmOke_N. To do it yourself, just read the file into an array with _FileReadToArray(), then loop through that array, splitting each line and putting the parts wherever you need them (a separate 2D array, for example). 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 Link to comment Share on other sites More sharing options...
lafafmentvotre Posted May 18, 2010 Author Share Posted May 18, 2010 Hello thanks for response How can i split each line in 2D array ? Link to comment Share on other sites More sharing options...
99ojo Posted May 18, 2010 Share Posted May 18, 2010 (edited) Hi, #include <file.au3> ;only needed for _arraydisplay #include <array.au3> Global $aritems ;read file into array _FileReadToArray (@ScriptDir & "\mytxt.txt", $aritems) ;Dim 2 d result array Dim $arresult [$aritems [0]] [2] ;loop over file array For $i = 1 To $aritems [0] ; Split string by "," and disabling of result return $temp = StringSplit ($aritems [$i], ",", 2) $arresult [$i - 1] [0] = $temp [0] $arresult [$i - 1] [1] = $temp [1] Next _ArrayDisplay ($arresult) ;-)) Stefan @edit: sitting in the corner donkey hat on my head .... Edited May 18, 2010 by 99ojo Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 18, 2010 Share Posted May 18, 2010 (edited) Create the 2D array with as many rows are there are lines in the file ([0] from the first array) and as many columns as you are interested in. Take each element from the first array (line from the file) and use StringSplit() to get that line broken into an array of parts. Copy the parts from the StringSplit array to your 2D array. If you get stuck, post what you've got and you'll get more help. Edit: Bad Stefan, he wanted to do it himself. No soup for you! Edited May 18, 2010 by PsaltyDS 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 Link to comment Share on other sites More sharing options...
zorphnog Posted May 18, 2010 Share Posted May 18, 2010 #include <Array.au3> Global $aTXT[4] $aTXT[0] = "computer1,108838" $aTXT[1] = "computer2,108836" $aTXT[2] = "computer3,108867" $aTXT[3] = "computer4,108825" Global $aRow, $iRows = UBound($aTXT), $iCols = 1, $aTable[1][1] For $i=0 To UBound($aTXT) - 1 $aRow = StringSplit($aTXT[$i], ",") If $aRow[0] > $iCols Then $iCols = $aRow[0] ReDim $aTable[$i+1][$iCols] For $j=1 To $aRow[0] $aTable[$i][$j-1] = $aRow[$j] Next Next _ArrayDisplay($aTable) Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 18, 2010 Share Posted May 18, 2010 Bad zorphnog. No soup for you either! Besides, if his array comes from _FileReadToArray() then $aTXT[0] is a count. Also, the 2D array should only be declared once and not need ReDim, since he already knows how many rows (from $aTXT[0]) and columns are of interest. 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 Link to comment Share on other sites More sharing options...
lafafmentvotre Posted May 18, 2010 Author Share Posted May 18, 2010 Thanks all for response PsaltyDS, i learn with the Stefan script and understand now how to work with array. Thanks Link to comment Share on other sites More sharing options...
99ojo Posted May 18, 2010 Share Posted May 18, 2010 Do i get now soup? Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 18, 2010 Share Posted May 18, 2010 Do i get now soup?Bread and water.(And you looked good in the donkey hat!) 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 Link to comment Share on other sites More sharing options...
zorphnog Posted May 18, 2010 Share Posted May 18, 2010 (edited) Bad zorphnog. No soup for you either! Besides, if his array comes from _FileReadToArray() then $aTXT[0] is a count. Also, the 2D array should only be declared once and not need ReDim, since he already knows how many rows (from $aTXT[0]) and columns are of interest. When did you become the soup nazi? Had I known that we could be paid in soup for posting I would've started posting much more often. Yes I know the ReDim's are not needed in this particular situation. My post was meant to be more of a teaching script on how to deal with variable length strings since a badly formed csv file could potentially cause out of bounds errors. I try not to assume anything when dealing with arrays. Although performance wise, the ReDimming should be frowned upon. Perhaps this would be better: #include <Array.au3> #include <File.au3> Global $aTXT _FileReadToArray(@ScriptDir & "\mytxt.txt", $aTXT) Global $aRow, $iRows = UBound($aTXT), $iCols = 2, $aTable[$aTXT[0]][2] For $i=1 To $aTXT[0] $aRow = StringSplit($aTXT[$i], ",") If $aRow[0] > $iCols Then $iCols = $aRow[0] ReDim $aTable[$aTXT[0]][$iCols] EndIf For $j=1 To $aRow[0] $aTable[$i-1][$j-1] = $aRow[$j] Next Next _ArrayDisplay($aTable) Edited May 18, 2010 by zorphnog 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