PDA

View Full Version : File Reading and separating "fields"



TheOne
11-03-2011, 00:24
Hi,

I was trying to read a comma delimited file and identify each element before the comma as a field. The comma is the separator so that you know you have encounter another field. For example (of the file content):

Name1,Phone Number1,ID number 1
Name2,Phone Number2,ID number 2
Name3,Phone Number3,ID number 3

So I was thinking of using PARSE for this:
Dim aFileinput() As String

Do While iEOF = 0
sInput = FILE_LineInput(FileHandle)
nLines = Parse(sInput,aFileinput(),",")
s1 = aFileinput(1)
s2 = aFileinput(2)
s3 = aFileinput(3)
Console_Write(s1 + $CRLF + s2 + $CRLF + s3 + $CRLF + $CRLF)
iEOF = FILE_EOF (FileHandle)
Loop

When I run it, I see all my input from the file print on the screen but then there is an error message:

400 Subscript out of range in array or matrix
Line 33
Line code s2 = aFileinput(2)

But it did print out aFileinput(2) and (3). I'm thinking that I'm not assessing the array correct to get to the "next field"?

Or is there a better way to do what I am trying to accomplish?

Thanks for your help again. Learning from the experts.

Petr Schreiber
11-03-2011, 01:16
Hi TheOne,

as for your code, it is possible you reached empty line. Did you checked nLines returned 3 at the moment?

Here is alternative way - you can load CSV file using single line of code to array, where first index will mean rows(lines) and second index columns.
(of course, the file must exist):


Uses "Console"

Dim sFileLines() As String

' nLines = number of lines found
' File says to parse command it is not parsing string, but the string is file name of file to be parsed
' $CRLF is end of line character
' "," defines per line separator
Dim nLines As Long = Parse(File "textFile.txt", sFileLines, $CRLF, ",")
Dim nLine, nColumn As Long

' Here we can reconstruct the file printing the contents from array
For nLine = 1 To nLines
For nColumn = 1 To UBound(sFileLines, 2)
Print sFileLines(nLine, nColumn)+","
Next
PrintL
Next

WaitKey


Let me know,
Petr

TheOne
11-03-2011, 02:25
Thanks Petr.

It looks like it will work.

As always, thank you.

I have another question. If you need to access the "2nd" column of the first "row" in the array and assign it to a variable (so that I can do something with it) - do you code it like this?

s1 = Array(1,2)

What I'm trying to do is to be able to parse each line and assign them to a variable. Then for each variable, I can check it, accumulate totals if it is a number or ignore that if I don't need to do anything.

For example:

CSV file:
NAME,AMOUNT,DATE
NAME,AMOUNT,DATE
NAME,AMOUNT,DATE

Read the file.
Assign first line name, amount and date to 3 variable.
Maybe accumulate the amount to another variable.
Loop till end of file.
Then I can print out the total or write to another file.

So not sure how the array works to assign a variable to each "column".

I hope this makes sense.

Thanks again for any insight.

Petr Schreiber
11-03-2011, 11:23
Hi,



I have another question. If you need to access the "2nd" column of the first "row" in the array and assign it to a variable (so that I can do something with it) - do you code it like this?

s1 = Array(1,2)


For the code I posted this should be the correct way.

I looked at your original code, and it can be modified this way to not crash on empty lines:


Uses "Console", "File"

Dim FileHandle As DWord
Dim aFileinput(), sInput, s1, s2, s3 As String
Dim nFields As Long

fileHandle = FILE_Open("textFile.txt", "INPUT")

While Not FILE_EOF(FileHandle)
sInput = FILE_LineInput(FileHandle)
nFields = Parse(sInput,aFileinput(),",")
If nFields > 2 Then
s1 = aFileinput(1)
s2 = aFileinput(2)
s3 = aFileinput(3)

PrintL(s1 + $CRLF + s2 + $CRLF + s3 + $CRLF(2))
Else
PrintL("Found line with less than 3 fields, skipping" + $CRLF(2))
End If
Wend

FILE_Close(FileHandle)
WaitKey


The problem with "bad addressing" in your original was caused by fact the PARSE automagically redims the array to number of fields. So in case of empty line it does not contain elements 2 and 3, and therefore it crashed.

This kind of problem can be easily eliminated by testing for the value of nFields.


Petr

TheOne
11-03-2011, 18:31
Thanks again Petr.

Learn something new. Didn't know it redim it. So I'll keep that in mind.
When I create my data to test, I probably added an extra blank line. So I'll bear that in mind as well. And it is a good idea to check for this error just in case.

My next "project" is to try to convert my data entry "console" code into a "UI". I hope you don't mind if I have questions later. The part I'm not sure yet is how to create and position the text and then the input "box" and so on. I'll keep reading the help pdf. :D

ErosOlmi
11-03-2011, 18:51
Also checks the examples. There are many for different areas.

Ciao
Eros