mary Posted June 7, 2009 Posted June 7, 2009 There are many exemples with StringRegExp but deals only about getting 1 Dim Array as result. I don't know if it is a limitation of regexp, but if not, would you please tell me how to parse csv fields with regexp ? the result must be a 2 Dim array.here is the csv string to parse:"010","2","03""24","30","20""txt","bla","toto"my code is:My_2Aarray=stringregexp($csv,'(".*?"),(".*?"),(".*?")',3)still give me 1 Dim Array
Valuater Posted June 8, 2009 Posted June 8, 2009 (edited) This is your example of the data to "Read" "010","2","03" "24","30","20" "txt","bla","toto" How are you visioning the data to "output" as a 2D array? $array[?][?] = ?? $array[0][1] = ?? etc 8) Edited June 8, 2009 by Valuater
PsaltyDS Posted June 8, 2009 Posted June 8, 2009 I don't think RegExp does anything 2D like that. You'll have to essentially StringSplit() each line of the file in a loop, and then do a 2D version of _ArrayConcatenate() onto the results array. PS: Here's a neat idea I just thought of, but haven't tried: Open the .csv as a database via Excel (I think ptrex posted an example of that), then use the ADO .getRows function to get the whole thing as a 2D array in one operation. It might even work! 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
Malkey Posted June 8, 2009 Posted June 8, 2009 (edited) Here is a function that converts CSV format to a 2D array. ; #include <array.au3> Opt("MustDeclareVars", 1) Dim $sCSV = '"010","2","03"' & @CRLF & _ '"24","30","20"' & @CRLF & _ '"txt","bla","toto"' Local $arr = _CsvToArray2D($sCSV) _ArrayDisplay($arr) ; Converts CSV format to a 2D array. Func _CsvToArray2D($sCSV) Local $aTmp = StringRegExp($sCSV & @CR, '(\V*)\v{1,2}', 3) Local $NumCols[UBound($aTmp)] For $x = 0 To UBound($aTmp) - 1 StringReplace($aTmp[$x], ",", ",") $NumCols[$x] = @extended + 1 Next Local $Max = _ArrayMax($NumCols, 1) Dim $aArr[UBound($aTmp)][$Max] For $i = 0 To UBound($aArr, 1) - 1 Local $aTemp = StringSplit($aTmp[$i], ",") For $j = 0 To $aTemp[0] - 1 $aArr[$i][$j] = $aTemp[$j + 1] Next Next Return $aArr EndFunc ;==>_CsvToArray2D ;========> End of _CsvToArray2D ====================== ; Using PsaltyDS's Excel idea, I have found _ExcelReadSheetToArray () works well once you get your head around what the parameters of the function do. Its reverse function, _ExcelWriteSheetFromArray(), should you need it, is also recommended. Both functions are in the help file with examples. Edit: Replaced this:- Local $sTmp = StringReplace($sCSV, @CR, @LF) $sTmp = StringReplace($sTmp, @LF & @LF, @LF) Local $aTmp = StringSplit($sTmp, @LF) Local $NumRows = $aTmp[0] _ArrayDelete($aTmp, 0) Local $NumCols[$NumRows] with this:- Local $aTmp = StringRegExp($sCSV & @CR, '(\V*)\v{1,2}', 3) Local $NumCols[uBound($aTmp)] Edited June 11, 2009 by Malkey
Tlem Posted June 8, 2009 Posted June 8, 2009 You can also take a look here.I made this to be sure that the number of elements do not stop the function or do not be forgotten and with it, I can use other files than CVS. Best Regards.Thierry
mary Posted June 10, 2009 Author Posted June 10, 2009 Thanks a lot for all your help ! So Regexp do not give 2D array natively
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