Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving ARRAY Data to CSV File to be used in different macro
#1
Hello All,

I am working on a project that has to steps. The first step I have already programed and is working really well however I need to pass the Array data from the first step to the second step. The macros are not setup to run right after each other because you may do the first step in the morning and then wait to do the second step until the afternoon. Anyway, I was trying to use the code below to pass the data to a CSV file and then when you are ready to run the second step the second macro will get the CSV file and ready it back into an array.

Also I thought I would mentiont that my user defined function, "QuoteDate" builds the array and has worked with every other macro I have written that uses this function.

Code:
Copy      Help
str s.getsel
str s1 s2 f
ARRAY(str) a.create(10)
QuoteData(a s)
f="$my qm$\test.csv"
s1=F"{a[0]},{a[1]},{a[2]},{a[3]},{a[4]},{a[5]},{a[6]},{a[7]},{a[8]},{a[9]},{a[10]}"
s1.setfile(f)

The problem is that I get a runtime error on line "s1=F"{a[0]},{a[1]}..." that says invalid index. Any help to solve this problem would be great.

Thanks,

Paul Daugs
#2
Function ArrayToCsvFile_str1d
Code:
Copy      Help
;/
function ARRAY(str)&a $_file

;Saves 1-dim str array to CSV file.


opt noerrorshere 1
ICsv x._create
for(_i 0 a.len) x.AddRowSA(-1 1 &a[_i])
x.ToFile(_file)
Function ArrayToCsvFile_str2d
Code:
Copy      Help
;/
function ARRAY(str)&a $_file

;Saves 2-dim str array to CSV file.


opt noerrorshere 1
ICsv x._create
x.FromArray(a)
x.ToFile(_file)
Function ArrayFromCsvFile_str1d
Code:
Copy      Help
;/
function ARRAY(str)&a $_file

;Loads 1-dim str array from CSV file.


opt noerrorshere 1
ICsv x._create
x.FromFile(_file)
a.create(x.RowCount)
for(_i 0 a.len) a[_i]=x.Cell(_i 0)
Function ArrayFromCsvFile_str2d
Code:
Copy      Help
;/
function ARRAY(str)&a $_file

;Loads 2-dim str array from CSV file.


opt noerrorshere 1
ICsv x._create
x.FromFile(_file)
x.ToArray(a)
#3
Hello Gintaras,

This was a wonderful solution. I changed my code to use your 2 delim array and it is working perfectly! However the one thing that I noticed is I can't add a new row to the csv file. How would I edit the code of the 2 delim array to also add rows so I can use this file as a history of quote run?

Thanks,

Paul Daugs
#4
Function ArrayToCsvFile_str2d2
Code:
Copy      Help
;/
function ARRAY(str)&a $_file [ARRAY(str)&aAppendRow]

;Saves 2-dim str array to CSV file.

;aAppendRow - optional 1-dim array containing data for a new row to append to the end of the CSV file.
;;;Array element count must be >=1 and <= CSV column count.


opt noerrorshere 1
ICsv x._create
x.FromArray(a)
if(&aAppendRow) x.AddRowSA(-1 aAppendRow.len &aAppendRow[0])
x.ToFile(_file)
#5
Hello Gintaras,

I am working through your code her to append the row and I am doing it with success. I guess my main problem is I don't understand what array variable I am passing to the aAppendRow optional variable? If you can help me with this I would greatly appreciate it.

All the Best,

Paul
#6
Macro Macro2534
Code:
Copy      Help
ARRAY(str) a b
str sFile="$temp$\a.txt"
;load an existing file and show the row count
if(FileExists(sFile)) ArrayFromCsvFile_str2d a sFile
out a.len
;append new row and save
b="qwe[]rty[]uio" ;;create array with 3 elements. You can instead use any other way to create array, for example: b.create(3); b[0]="qwe"; b[1]="rty"; b[2]="uio"
ArrayToCsvFile_str2d2 a sFile b
;show results
run sFile

;run this macro several times to see how it works

Or you can add rows directly to array a. Then don't need two arrays.
Macro Macro2535
Code:
Copy      Help
ARRAY(str) a
str sFile="$temp$\a2.txt"
;load an existing file and show the row count
if(FileExists(sFile)) ArrayFromCsvFile_str2d a sFile
out a.len
;append new row and save
int i=a.len
if(i) a.redim(i+1) ;;add one row
else a.create(3 1) ;;create array with 3 columns and 1 row
a[0 i]="qwe"; a[1 i]="rty"; a[2 i]="uio"
ArrayToCsvFile_str2d a sFile
;show results
run sFile

;run this macro several times to see how it works
#7
Hello Gintaras,

I tried running the macro but I am getting a unknown identifier error on "ArrayFromCsvFile_str2d". Am I missing some classes of code that should be in my code library?

Paul
#8
Saving ARRAY Data to CSV File to be used in different macro


Forum Jump:


Users browsing this thread: 1 Guest(s)