Posts: 197
Threads: 60
Joined: Dec 2013
Hi,
My csv has a single column of values that I wish to save into an array using v.ToArray
I took the code ( ToArray(ARRAY(str)*a)) from the help file but it says "Error in Macro: ARRAY variable cannot be declared inline."
so I tried the following but the array isn't fully filled it seems
int nr=v.RowCount
int nc=v.ColumnCount
ARRAY(str) a.create(nc nr)
v.ToArray(a)
Thanks for reading.
Posts: 12,090
Threads: 142
Joined: Dec 2002
ToArray(ARRAY(str)*a) is declaration, not example.
Please give working code to test and reproduce the problem (array not fully filled). With short CSV for testing.
Posts: 197
Threads: 60
Joined: Dec 2013
Here you go,
Please rename the attachment to test.csv. The attachment uploader won't let me upload csv.
str s ss
ICsv v._create
v.FromFile("c:\test\test.csv")
int nr=v.RowCount
int nc=v.ColumnCount
int r c
for r 0 nr
;out "--- row %i ---" r
for c 0 nc
s=v.Cell(r c)
;out s
v.ToString(ss)
;out ss
;v.ToArray(ARRAY(str)*a) Error in Macro: ARRAY variable cannot be declared inline.
ARRAY(str) a.create(nc nr)
v.ToArray(a)
for r 0 nr
;out "--- row %i ---" r
for c 0 nc
out a[r c]
;out s
Attached Files
test.zip (Size: 550 bytes / Downloads: 416)
Posts: 12,090
Threads: 142
Joined: Dec 2002
For testing please use string, not file. Example:
str s=
;line1
;line2
s.setfile("$temp qm$\test.csv")
...
v.FromFile("$temp qm$\test.csv")
Also, please post valid colored code. Now if I copy/paste it will not work.
Posts: 197
Threads: 60
Joined: Dec 2013
k. I tried doing that, but now the macro breaks after outputting "line1"
There's an error with this line "out a[r c]" near the bottom <-------- Error (RT) in reading csv - getting help in forum: invalid index. ?
Here's the code
Macro
reading csv - getting help in forum
str s ss
s=
;line1
;line2
;line3
;line4
s.setfile ( "$temp qm$\test.csv" )
ICsv v._create
v.FromFile ( "$temp qm$\test.csv" )
int nr= v.RowCount
int nc= v.ColumnCount
int r c
for r 0 nr
, ;out "--- row %i ---" r
, for c 0 nc
,, s= v.Cell ( r c)
,, ;out s
v.ToString ( ss)
;out ss
;v.ToArray(ARRAY(str)*a) Error in Macro: ARRAY variable cannot be declared inline.
ARRAY ( str ) a.create ( nc nr)
v.ToArray ( a)
for r 0 nr
, ;out "--- row %i ---" r
, for c 0 nc
,, out a[r c]
,, ;out s
Posts: 12,090
Threads: 142
Joined: Dec 2002
replace
out a[r c]
to
out a[c r]
In 2-dim arrays, the last dimension usually is for rows.
I'll add this note to ToArray help.
Macro
Macro2209
out
str s ss
s=
;line1
;line2
;line3
;line4
s.setfile ( "$temp qm$\test.csv" )
ICsv v._create
v.FromFile ( "$temp qm$\test.csv" )
ARRAY ( str ) a
v.ToArray ( a)
int r c
for r 0 a.len
, ;out "--- row %i ---" r
, for c 0 a.len ( 1 )
,, out a[c r]
;or
for r 0 a.len
, out a[0 r]
Posts: 197
Threads: 60
Joined: Dec 2013
Thank you!
Works perfectly.