Sign in to follow this  
Followers 0
Delta

Converting a csv file into a 2D array

8 posts in this topic

I'm trying to convert a CSV file with 4 columns and n number of rows in to a 2D array. I see no reason why the code below doesn't work but I think I need a second opinion.

$csv = FileRead("itr-data.csv")
$csv = StringStripWS($csv, 7)
$rows = StringSplit($csv, @CRLF)

Dim $entries[$rows[0]][4]
$entries[0][0] = $rows[0]

For $i = 1 to $rows[0]
    $temp = StringSplit($rows[$i], ",", 2)
    For $j = 0 to UBound($temp) - 1
        $entries[$i][$j] = $temp[$j] ; : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
    Next
Next

[size="1"]Please stop confusing "how to" with "how do"[/size]

Share this post


Link to post
Share on other sites



Your CSV text probably has a line that generates a case that returns more than 4 columns.

-> $entries[$rows[0]][4] ;; hardcoded limit of 4 columns for CSV data per line.

$temp = StringSplit($rows[$i], ",", 2)
;; debug.
If Not IsArray($temp) Then
    ;; output a message.
ElseIf UBound($temp, 1) > 4 Then
    ;; output a other message.
EndIf

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Unfortunately that's not the case here. I tried your test. $temp is an array and is not greater than 4.


[size="1"]Please stop confusing "how to" with "how do"[/size]

Share this post


Link to post
Share on other sites

Did you try for each row? Because when I look at this I come to the same conclusion.

Could you include the cvs file, or a cvs file with meaningless contant, that produces the error?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

See next message.

(I knew there is a reason I don't like to mix indexed and none indexed array's.)

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

This should work:

#include <Array.au3>
$csv = FileRead("itr-data.csv")
$csv = StringStripWS($csv, 7)
$rows = StringSplit($csv, @CRLF)
Dim $entries[$rows[0] + 1][4 + 1]
$entries[0][0] = $rows[0]
For $i = 1 to $rows[0] 
    $temp = StringSplit($rows[$i], ",", 2) 
    For $j = 0 to UBound($temp) - 1 
        $entries[$i][$j] = $temp[$j] ;
    Next
Next
_ArrayDisplay($entries)

The array dimensions were set wrongly.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

When I began working with CSV files, I found that my major hangup would be that some CSV lists are formed with quotation marks wrapped to each element, which causes problems with string formating. If your csv file contains these quotation marks, I suggest doing a StringReplace to get rid of the quotation marks before spliting the string.


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

Thank UEZ that fixed it. What actually fixed it was $entries[$rows[0] + 1][4]. If I used $entries[$rows[0] + 1][4+1] I got an extra unused column which is fine but I didn't want that.


[size="1"]Please stop confusing "how to" with "how do"[/size]

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  
Followers 0