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 (6)

  1. Calvin,

    1. When calculating line length, don’t you also need to take into consideration logical lines of code that split across multiple phyiscal lines of code? In other words, lines that are terminated with ‘;’ that wrap around to the next line?

    2. Would it make sense to alltrim() spaces and tabs before calculating line lengths?

    I enjoy your blog!

    Malcolm

  2. RyanNerd says:

    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)

  3. Franklin says:

    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 "

  4. Franklin says:

    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 "

  5. Brad Schulz says:

    (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

  6. Maros says:

    Hello Calvin, next code crash VFP9.

    *c Lubomír Ježek, jezek at jkr dot cz

    * crach VFP7 and 9 on locate or brow

    CLOSE ALL

    CLEAR all

    USE tst

    z=CHR(1)

    *

    hled="like(‘*12345678/0800*’, Upper(z+’ ‘+NVL(IDENT1,”)+’ ‘+z+’ ‘+NVL(RAZITKO1,”)+’ ‘+z+’ ‘"+;

    "+NVL(VARIA,”)+’ ‘+z+NVL(DTOC(DAT_VYPISU),”)+z+’ ‘+NVL(CBV,”)+’ ‘+z+’ ‘+NVL(PROVEDL,”)+’ ‘+z+’ ‘+"+;

    "NVL(VE_PROS,”)+’ ‘+z+’ ‘+NVL(KUR_MENA,”)+’ ‘+z+’ ‘+NVL(NA_VRUB,”)+’ ‘+z+’ ‘+NVL(KONST,”)+’ ‘+z+’ ‘+"+;

    "NVL(SPEC,”)+’ ‘+z+NVL(DTOC(DAT_PLATBY),”)+z+NVL(DTOC(DEN_SPL),”)+z+’ ‘+NVL(CDOKLAD,”)+’ ‘+z+’ ‘+"+;

    "NVL(UC_MD,”)+’ ‘+z+’ ‘+NVL(STRED_MD,”)+’ ‘+z+’ ‘+NVL(UC_D,”)+’ ‘+z+’ ‘+NVL(STRED_D,”)+’ ‘+z+’ ‘+"+;

    "NVL(F_VNAME,”)+’ ‘+z+’ ‘+NVL(AUTO,”)+’ ‘+z+’ ‘+NVL(CODE,”)+’ ‘))"

    *this is OK

    LOCATE FOR &hled

    hled2=;

    "like(‘*12345678/0800*’, Upper(CHR(1)+’ ‘+NVL(IDENT1,”)+’ ‘+CHR(1)+’ ‘+NVL(RAZITKO1,”)+’ ‘+CHR(1)+"+;

    "’ ‘+NVL(VARIA,”)+’ ‘+CHR(1)+NVL(DTOC(DAT_VYPISU),”)+CHR(1)+’ ‘+NVL(CBV,”)+’ ‘+CHR(1)+’ ‘+NVL(PROVEDL,”)+"+;

    "’ ‘+CHR(1)+’ ‘+NVL(VE_PROS,”)+’ ‘+CHR(1)+’ ‘+NVL(KUR_MENA,”)+’ ‘+CHR(1)+’ ‘+NVL(NA_VRUB,”)+’ ‘+CHR(1)+’ ‘"+;

    "+NVL(KONST,”)+’ ‘+CHR(1)+’ ‘+NVL(SPEC,”)+’ ‘+CHR(1)+NVL(DTOC(DAT_PLATBY),”)+CHR(1)+NVL(DTOC(DEN_SPL),”)+"+;

    "CHR(1)+’ ‘+NVL(CDOKLAD,”)+’ ‘+CHR(1)+’ ‘+NVL(UC_MD,”)+’ ‘+CHR(1)+’ ‘+NVL(STRED_MD,”)+’ ‘+CHR(1)+’ ‘+NVL(UC_D,”)"+;

    "+’ ‘+CHR(1)+’ ‘+NVL(STRED_D,”)+’ ‘+CHR(1)+’ ‘+NVL(F_VNAME,”)+’ ‘+CHR(1)+’ ‘+NVL(AUTO,”)+’ ‘+CHR(1)+’ ‘"+;

    "+NVL(CODE,”)+’ ‘))"

    *LOCATE crash, browse crash :

    LOCATE FOR &hled2

    GO top

    BROWSE FIELDS RAZITKO1,po=Upper(CHR(1)+’ ‘+NVL(IDENT1,”)+’ ‘+CHR(1)+’ ‘+NVL(RAZITKO1,”)+’ ‘+CHR(1)+’ ‘+NVL(VARIA,”)+’ ‘+CHR(1)+NVL(DTOC(DAT_VYPISU),”)+CHR(1)+’ ‘+NVL(CBV,”)+’ ‘+CHR(1)+’ ‘+NVL(PROVEDL,”)+’ ‘+CHR(1)+’ ‘+NVL(VE_PROS,”)+’ ‘+CHR(1)+’ ‘+NVL(KUR_MENA,”)+’ ‘+CHR(1)+’ ‘+NVL(NA_VRUB,”)+’ ‘+CHR(1)+’ ‘+NVL(KONST,”)+’ ‘+CHR(1)+’ ‘+NVL(SPEC,”)+’ ‘+CHR(1)+NVL(DTOC(DAT_PLATBY),”)+CHR(1)+NVL(DTOC(DEN_SPL),”)+CHR(1)+’ ‘+NVL(CDOKLAD,”)+’ ‘+CHR(1)+’ ‘+NVL(UC_MD,”)+’ ‘+CHR(1)+’ ‘+NVL(STRED_MD,”)+’ ‘+CHR(1)+’ ‘+NVL(UC_D,”)+’ ‘+CHR(1)+’ ‘+NVL(STRED_D,”)+’ ‘+CHR(1)+’ ‘+NVL(F_VNAME,”)+’ ‘+CHR(1)+’ ‘+NVL(AUTO,”)+’ ‘+CHR(1)+’ ‘+NVL(CODE,”)+’ ‘)

    *like(‘*12345678/0800*’, Upper(CHR(1)+’ ‘+NVL(IDENT1,”)+’ ‘+CHR(1)+’ ‘+NVL(RAZITKO1,”)+’ ‘+CHR(1)+’ ‘+NVL(VARIA,”)+’ ‘+CHR(1)+NVL(DTOC(DAT_VYPISU),”)+CHR(1)+’ ‘+NVL(CBV,”)+’ ‘+CHR(1)+’ ‘+NVL(PROVEDL,”)+’ ‘+CHR(1)+’ ‘+NVL(VE_PROS,”)+’ ‘+CHR(1)+’ ‘+NVL(KUR_MENA,”)+’ ‘+CHR(1)+’ ‘+NVL(NA_VRUB,”)+’ ‘+CHR(1)+’ ‘+NVL(KONST,”)+’ ‘+CHR(1)+’ ‘+NVL(SPEC,”)+’ ‘+CHR(1)+NVL(DTOC(DAT_PLATBY),”)+CHR(1)+NVL(DTOC(DEN_SPL),”)+CHR(1)+’ ‘+NVL(CDOKLAD,”)+’ ‘+CHR(1)+’ ‘+NVL(UC_MD,”)+’ ‘+CHR(1)+’ ‘+NVL(STRED_MD,”)+’ ‘+CHR(1)+’ ‘+NVL(UC_D,”)+’ ‘+CHR(1)+’ ‘+NVL(STRED_D,”)+’ ‘+CHR(1)+’ ‘+NVL(F_VNAME,”)+’ ‘+CHR(1)+’ ‘+NVL(AUTO,”)+’ ‘+CHR(1)+’ ‘+NVL(CODE,”)+’ ‘))

Skip to main content