Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] csv - v.toarray problem
#1
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.
#2
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.
#3
Here you go,

Please rename the attachment to test.csv. The attachment uploader won't let me upload csv.

Code:
Copy      Help
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
.zip   test.zip (Size: 550 bytes / Downloads: 416)
#4
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.
#5
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
Code:
Copy      Help
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
#6
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
Code:
Copy      Help
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]
#7
Thank you!

Works perfectly.


Forum Jump:


Users browsing this thread: 2 Guest(s)