Page 1 of 1

Counting fields in a record

Posted: Sat Jan 30, 2010 2:03 am
by dbish
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

Posted: Sat Jan 30, 2010 2:08 am
by dbish
Note to moderator - if this should be posted in the Qbasic forum please feel free to move/copy.

Thanks

Posted: Sat Jan 30, 2010 6:39 pm
by burger2227
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.

Posted: Mon Feb 01, 2010 10:17 am
by dbish
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.)

Posted: Mon Feb 01, 2010 1:29 pm
by burger2227
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.

Posted: Mon Feb 01, 2010 3:10 pm
by dbish
Thanks - this will work great!

Posted: Mon Apr 19, 2010 10:01 pm
by canarymu
good job! thank for sharing!! :D :D
__________________
Watch The Losers Movie Online Free