[futurebasic] Re: [FB] COUNTFIELDS

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2001 : Group Archive : Group : All Groups

From: Jay Reeve <jktr@...>
Date: Sun, 21 Oct 01 09:22:40 -0500
Subject:     Re: [FB] COUNTFIELDS
Sent:        10/20/01 11:56 PM
To:          FB list, futurebasic@...

>CLEAR LOCAL FN CountFields( inputStr AS STR255, separatorStr AS STR255 )
>DIM fields AS STR255
>DIM result AS LONG
>
>FN Split$( inputStr, separatorStr, 0 )
>result = FN UBOUND( splitWords ) + 1
>
>END FN = result

Unless you really want to go ahead and split any found fields, all FN 
CountFields needs to do is this. (My version of FN SPLIT already returns 
the count.)

LOCAL FN CountFields( inputStr AS STR255, separatorStr AS STR15 )
DIM AS UNSIGNED BYTE r, c
r = inputStr[0]
IF r = 0 THEN c = r : EXIT FN
c = 1
WHILE r
IF inputStr[r] = separatorStr[1] THEN c ++
r --
WEND
END FN = c

Your FN SPLIT$ returns a specific field. I would propose a different FN 
extractField, as it is unnecessary to build an array to extract a single, 
specific field:

LOCAL FN extractField$( inputStr AS STR255, delim AS char, whichFld )
DIM AS UNSIGNED BYTE r, fCount, L, fBeg
L = inputStr[0]
fBeg = 0
IF L = 0 THEN EXIT FN
fCount = 0
FOR r = 1 TO L
LONG IF inputStr[r] = delim
fCount ++
IF fCount = whichFld then inputStr[fBeg] = r - fBeg - 1 : exit fn
fBeg = r
END IF
NEXT
IF fCount + 1 = whichFld then inputStr[fBeg] = L - fBeg : exit fn
inputStr[fBeg] = 0
END FN = PSTR$(@inputStr[fBeg])

dim i,S$
S = "won,too,free,for,5,the joy of 6,heaven,ate,nein,tin,"
for i = 0 to 11
print  "Field";i;" = ";"""";
print fn extractField$(S,_",",i);
print """"
next

I acknowledge this may not be the way it functions in other BASICs, but 
to me it's just more logical.

One other comment: In your FN SPLIT$, you begin by killing the dynamic 
array. Why not use the already allocated memory, if any, rather than 
making the memory manager start over again? It's not like you have any 
other need for that memory during the FN, and you already compress 
dynamic to release any excess allocation at the end.

 0"0
 =J= a  y
  "