Results 1 to 6 of 6

Thread: File Reading and separating "fields"

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    20
    Rep Power
    16

    File Reading and separating "fields"

    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.

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    20
    Rep Power
    16
    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.

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    20
    Rep Power
    16
    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.

  6. #6
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10
    Also checks the examples. There are many for different areas.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Similar Threads

  1. Uses "File", "Crypto" ... ???
    By marcuslee in forum thinBasic General
    Replies: 3
    Last Post: 01-12-2009, 19:38

Members who have read this thread: 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •