Counting fields in a record

The forum for all of your Freebasic needs!

Moderators: Pete, Mods

dbish
Coder
Posts: 19
Joined: Tue Apr 12, 2005 7:37 pm

Counting fields in a record

Postby dbish » Sat Jan 30, 2010 2:03 am

I am using the FreeBasic compiler but I suspect that Qbasic techniques would also help me here.

My FB programs currently read in a CSV file that is approximately 300,000 rows by exactly 19 data items per row. I first use a Line Input routine to determine the number of rows and then read the file sequentially into memory where I then do manipulations and save as a new file.

===============
i = 0
OPEN FILENAMECURRENT$ FOR INPUT AS #1
DO WHILE NOT EOF(1)
i = i + 1
LINE INPUT #1, OPTIONLINE$
CHAR1$ = LEFT$(OPTIONLINE$, 1)
IF CHAR1$ = "" THEN EXIT DO
LOOP
CLOSE #1
FINALROWNUMBER_MASTER% = i
OPEN FILENAMECURRENT$ FOR INPUT AS #1
FOR i = 1 TO FINALROWNUMBER_MASTER%
FOR j = 1 TO 19
INPUT #1, WorkingRawArray$(i, j)
NEXT
NEXT
CLOSE #1
======================

My data vendor now tells me that due to extraneous events it is possible that some rows will have 19 items and some will have 20 items. I think I want to do the same line input routine to determine the number of rows but then read each line in one at a time. For rows with 19 items I will add an extra +",DUMMY" value at the end to cause all rows to be the same data item length. I can probably figure that part out myself.

My problem is what would be a good way to determine whether there are 19 or 20 data items in the row?

Any help would be appreciated.

Thanks

dbish

dbish
Coder
Posts: 19
Joined: Tue Apr 12, 2005 7:37 pm

Postby dbish » Sat Jan 30, 2010 2:08 am

Note to moderator - if this should be posted in the Qbasic forum please feel free to move/copy.

Thanks

User avatar
burger2227
Veteran
Posts: 2143
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Postby burger2227 » Sat Jan 30, 2010 6:39 pm

Does the data use commas between each value like a WRITE file would?

Count the commas if so by scanning the LINE INPUT text string with MID$.

"23,456,467,22" ' and add 1 to the final count

Commas would not follow the last entry.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : http://www.QB64.NET
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

dbish
Coder
Posts: 19
Joined: Tue Apr 12, 2005 7:37 pm

Postby dbish » Mon Feb 01, 2010 10:17 am

Good Idea. I was hoping there was a single command like PARSE (I just made that up) or something that would do it in one pass but this could work.

I am assuming I would do this in some sort of loop like:

COMMACOUNT=0
FOR i = 1 to LINEDATALENGTH-1
if MID$(LINEDATA,i,i+1)="," Then COMMACOUNT=COMMACOUNT+1
Next i

Is this what you were thinking? (I can fine tune the code - just a concept question.)

User avatar
burger2227
Veteran
Posts: 2143
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Postby burger2227 » Mon Feb 01, 2010 1:29 pm

Almost... you just want to read one character at a time so the MID$ length value is always 1. Otherwise it will never equal one comma:

Code: Select all

DO UNTIL EOF(1)
  LINE INPUT #1, dataline$
  IF LEN(dataline$) THEN   'data must have a length
     datacount = 1  'assume one piece of data w/o comma
     FOR d = 1 TO LEN(dataline$)
        IF MID$(dataline$, d, 1) = "," THEN datacount = datacount + 1
     NEXT
  ELSE : datacount = 0 ' no data on file line
  END IF
  PRINT dataline$, datacount
    ' your other code here
LOOP


You can place the data and counts into arrays for later use.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : http://www.QB64.NET
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

dbish
Coder
Posts: 19
Joined: Tue Apr 12, 2005 7:37 pm

Postby dbish » Mon Feb 01, 2010 3:10 pm

Thanks - this will work great!

canarymu
Newbie
Posts: 2
Joined: Mon Apr 19, 2010 11:23 am

Postby canarymu » Mon Apr 19, 2010 10:01 pm

good job! thank for sharing!! :D :D
__________________
Watch The Losers Movie Online Free


Return to “Freebasic Questions & Answers”

Who is online

Users browsing this forum: No registered users and 2 guests

cron