Author Topic: Comma delimited text file  (Read 158 times)

oradba4u

  • Jr. Member
  • **
  • Posts: 86
Comma delimited text file
« on: February 16, 2021, 12:45:05 am »
All:
I am attempting to read an ASCII text file with 10 items per line (except the 1st line), separated by commas (no other commas in the line - meaning no commas in the actual data).

I have a routine that counts the number of lines, but whenever I try to put the values in an array, I get error "Uninitialized array element".

I have also tried using StrSub and StrScan on an individual line of data and parse it our to 10 variables! I can't seem to get the knack of parsing it this way.
P.S. The first line of the data file contains the name of the data file. Other than that, I need to extract data item #1, #8, and #9

Frustration is growing, and it's late at night, so I'll leave it at that.

Data file looks something like this:

Line 1 - Data File
1,2,3,4,5,6,7,8,9,10
2,3,4,,5,,6,7,,8,9,10,1
3,4,5,6,7,8,9,10,1,2
4,5,6,7,8,9,10,1,2,3
5,6,7,8,9,10,1,2,3,4
6,7,8,9,10,1,2,3,4,5

stanl

  • Pundit
  • *****
  • Posts: 1390
Re: Comma delimited text file
« Reply #1 on: February 16, 2021, 02:29:05 am »
Might not be optimal, but  FileRead() and ItemExtract() will work.

JTaylor

  • Pundit
  • *****
  • Posts: 1595
    • Data & Stuff Inc.
Re: Comma delimited text file
« Reply #2 on: February 16, 2021, 12:04:14 pm »
Guessing your problem stems from varying number of columns in the rows which is resulting in empty/uninitialized array elements but since you didn't post your code it is hard to say what you did.

jim

stanl

  • Pundit
  • *****
  • Posts: 1390
Re: Comma delimited text file
« Reply #3 on: February 16, 2021, 12:37:43 pm »
like I said, FileRead() and ItemExtact() will work even with varying columns as long as there is at least 10. Now the question is: is this optimal... harking back to the how to streamline my code.
Code: Winbatch

;Data saved as text file
;Line 1 - Data File
;1,2,3,4,5,6,7,8,9,10
;2,3,4,,5,,6,7,,8,9,10,1
;3,4,5,6,7,8,9,10,1,2
;4,5,6,7,8,9,10,1,2,3
;5,6,7,8,9,10,1,2,3,4
;6,7,8,9,10,1,2,3,4,5
cFile = dirscript():"csv_ar_test.txt"
arr = ArrDimension(3)
h= FileOpen(cFile,"READ")
line = FileRead(h) ;skip first line
While @TRUE ; Loop till break do us end
   line = FileRead(h)
   If line == "*EOF*" Then Break
   a1 = ItemExtract(1,line,",")
   a8 = ItemExtract(8,line,",")
   a9 = ItemExtract(9,line,",")
   Message("",a1:@LF:a8:@LF:a9) ;just checks elements entered, includes blanks
   ArrayInsert(arr,1,1,a1)
   ArrayInsert(arr,2,1,a8)
   ArrayInsert(arr,3,1,a9)


EndWhile
FileClose(h)


;pretty worthless but what the hey....
Message("Array Values",ArrayToStr(arr))




 

td

  • Tech Support
  • *****
  • Posts: 3634
    • WinBatch
Re: Comma delimited text file
« Reply #4 on: February 16, 2021, 02:02:52 pm »
There's always ArrayFileGetCSV.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1390
Re: Comma delimited text file
« Reply #5 on: February 17, 2021, 02:40:04 am »
There's always ArrayFileGetCSV.


Would have been my first choice. Wasn't sure how the first line with no commas and the potential variable columns would be treated. Also, we are begging the question of what is the purpose of an array except for 'can it be done'? Another homework assignment ???

td

  • Tech Support
  • *****
  • Posts: 3634
    • WinBatch
Re: Comma delimited text file
« Reply #6 on: February 17, 2021, 07:25:02 am »
The function would have no problem with the first line unless it contains a bunch of CSV special-meaning characters arranged in a CSV rule violating way.  As for the OP's motivation, don't care much.  The suggestion is directed as a hint for any WinBatch users passing by.  There is so much in Winbatch that it is easy to overlook functionality.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1390
Re: Comma delimited text file
« Reply #7 on: February 17, 2021, 11:35:30 am »
The function would have no problem with the first line unless it contains a bunch of CSV special-meaning characters arranged in a CSV rule violating way.  As for the OP's motivation, don't care much.  The suggestion is directed as a hint for any WinBatch users passing by.  There is so much in Winbatch that it is easy to overlook functionality.


As for the OP's motivation, I agree and don't care much either. And if the ask is to construct a 3-element array from a .csv with a header row and variable column sizes, I'm sure there are several options.

stanl

  • Pundit
  • *****
  • Posts: 1390
Re: Comma delimited text file
« Reply #8 on: February 18, 2021, 02:51:01 am »
using ArrayFileGetCSV:
Code: Winbatch

cFile = dirscript():"csv_ar_test.txt"
array = ArrayFileGetCSV(cFile, 0, ",", 0, 0)
ArrayRemove( array, 0, 1)
rowcount = ArrInfo(array,1)
colcount = ArrInfo(array,2)
Message(StrCat("Number of Rows in the file ",rowcount), StrCat("Number of Columns in the file ",colcount))


values=""
For i = 0 to rowcount -1
   values = values:array[i,0]:",":array[i,7]:",":array[i,8]:@LF
Next


Message("Array Values",values)
 

oradba4u

  • Jr. Member
  • **
  • Posts: 86
Re: Comma delimited text file
« Reply #9 on: February 18, 2021, 03:26:51 am »
Thanks for the assistance... I solved the problem by creating a subroutine that reads the first line, and then looping until EOF in the remaining rows:

:GetData
Handle=FileOpen("MyData.txt","READ")
Line = FileRead(Handle)

while @TRUE ; Loop until EOF
Line = FileRead(Handle)
If Line == "*EOF*" Then Break

; Parse data...
Val1=ItemExtractCSV(1,Line,0,",")
Val2=ItemExtractCSV(2,Line,0,",")
Val8=ItemExtractCSV(8,Line,0,",")
Val9=ItemExtractCSV(9,Line,0,",")

; additional calculations...

return

stanl

  • Pundit
  • *****
  • Posts: 1390
Re: Comma delimited text file
« Reply #10 on: February 18, 2021, 07:31:18 am »
Given the text file may have variable column count, you might consider a little safety net
Code: Winbatch

maxcol=9
If Line == "*EOF*" Then Break
   If ItemCount(line,",") >= maxcol
      ; Parse data...
      Val1=ItemExtractCSV(1,Line,0,",")
      Val2=ItemExtractCSV(2,Line,0,",")
      Val8=ItemExtractCSV(8,Line,0,",")
      Val9=ItemExtractCSV(maxcol,Line,0,",")
   Endif