Jump to content
Sign in to follow this  
mary

csv to 2D array with regexp

Recommended Posts

mary

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 :D

Share this post


Link to post
Share on other sites
Valuater

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 by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites
PsaltyDS

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.

:D

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!

:D


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

Share this post


Link to post
Share on other sites
Malkey

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 by Malkey

Share this post


Link to post
Share on other sites
Tlem

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. :D


Best Regards.Thierry

Share this post


Link to post
Share on other sites
mary

Thanks a lot for all your help !

So Regexp do not give 2D array natively :D

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.