Using Foxpro to query line lengths
I was reviewing some code changes, and I noticed some of the lines were quite different in length.
I wrote some code to figure out why. I thought initially that I would just create a cursor with a field of length 250 and then use the APPEND FROM …SDF command to put the entire file into a cursor. However, I realized that wouldn’t work because one of the lines was several thousand characters long. So I thought I’d had to use the low level file functions, such as FREAD(). I also thought about appending the entire file into a memo field using the APPEND MEMO command
Then I remembered the ALINES function which served the job quite nicely. I just wanted to show the longest line lengths before and after modifications.
cPath="d:\public\files\"
SET ALTERNATE TO t.txt
SET ALTERNATE on
ShowLens(cPath+"Samples.cpp")
ShowLens(cPath+"Samples2.cpp")
SET ALTERNATE to
_cliptext=FILETOSTR("t.txt")
PROCEDURE ShowLens(cFile as String)
cStr=FILETOSTR(cFile)
n=ALINES(aa,cStr)
CREATE CURSOR lengths (Line i, len i)
FOR i = 1 TO n
INSERT INTO lengths VALUES (i,LEN(aa[i]))
ENDFOR
SELECT TOP 10 line,len FROM lengths ORDER BY 2 DESC INTO CURSOR result
?JUSTFNAME(cFile), "total lines = ",n
LIST off
RETURN
Comments
- Anonymous
April 20, 2006
The comment has been removed - Anonymous
April 20, 2006
VFPs string manipulating and parsing functions are some of the best of any computer language out there.
I'm curious about VFPs internal parsing engine. Did you use LEX or YAK code in the compiler?
Cheers,
-Ryan(Nerd) - Anonymous
April 20, 2006
hi, I like your blog is very good.
Add at this with size of fields for example:
select nombre,iif(valor=1,textocorto,textolargo) as textos from mitablas
result:
Pepe - "abcd"
Jose - "yzxa"
If the first fields is textocorso for the condition, the more records have only the lenght of the first result, but if the second record have textolargo for the condition, the result is that information is truncated, for this reason I use this:
select nombre,STUFF(SPACE(15),1,15,iif(valor=1,textocorto,textolargo)) as textos from mitablas
result:
Pepe - "abcd "
Jose - "yzxadfghjklñpo " - Anonymous
April 20, 2006
hi, I like your blog is very good.
Add at this with size of fields for example:
select nombre,iif(valor=1,textocorto,textolargo) as textos from mitablas
result:
Pepe - "abcd"
Jose - "yzxa"
If the first fields is textocorso for the condition, the more records have only the lenght of the first result, but if the second record have textolargo for the condition, the result is that information is truncated, for this reason I use this:
select nombre,STUFF(SPACE(15),1,15,iif(valor=1,textocorto,textolargo)) as textos from mitablas
result:
Pepe - "abcd "
Jose - "yzxadfghjklñpo " - Anonymous
April 20, 2006
(To Franklin)
Instead of this:
select nombre,STUFF(SPACE(15),1,15,iif(valor=1,textocorto,textolargo)) as textos from mitablas
You may want to use this (which is faster and arguably clearer):
select nombre,PADR(iif(valor=1,textocorto,textolargo),15) as textos from mitablas
I was also going to suggest the CAST() function:
select nombre,CAST(iif(valor=1,textocorto,textolargo) as C(15)) as textos from mitablas
But I was kinda surprised to see that it's the slowest of the three.
--Brad - Anonymous
April 24, 2006
The comment has been removed