Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Reverse comma-delimited string
Message
De
04/09/2019 11:15:09
Mike Yearwood
Toronto, Ontario, Canada
 
 
À
04/09/2019 09:43:29
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01670542
Message ID:
01670571
Vues:
76
Pretty cool! Others would be accusing me of wasting time worrying about .46s vs .75s.

In the interest of scientific analysis, if you're going to compare mdot versus non-mdot, you have to include a getwordnum version with mdot. Also what is the result with a string consisting of words one to one-hundred?

I take the don't repeat yourself (DRY) principal farther than most. I'd create a string variable for the expected input and result and have all the functions compare use those strings, rather than copy/pasting the expected input/result.


>>Just want to add that in my testing, ALINES() are much, much faster than the GETWORD*() functions. For a short string, it probably won't make much difference, but done repeatedly or for long strings, it'll be visible. (Mike, I know you know this; adding it for Rick.)
>
>I didn't exhaustively check this code. It may have bugs. I also altered Walter's algorithm slightly to provide a comma-space between each item. And on each item, I only process if a comma is found somewhere in the string.
>
>
* Averages:
>* 0.74s ALINES() m.
>* 0.75s ALINES()
>* 0.46s GETWORDNUM() Rick
>* 0.46s GETWORDNUM() Walter
>
>
>* Test code:
>CLEAR
>DIMENSION laAlinesM[5]
>DIMENSION laAlines[5]
>DIMENSION laGetWordNumR[5]
>DIMENSION laGetWordNumW[5]
>
>IF reverse_alines_mdot("one, two, three") != "three, two, one"
>    ? "reverse_alines_mdot() fails test"
>ENDIF
>IF reverse_alines("one, two, three") != "three, two, one"
>    ? "reverse_alines() fails test"
>ENDIF
>IF reverse_getwordnum_rick("one, two, three") != "three, two, one"
>    ? "reverse_getwordnum_rick() fails test"
>ENDIF
>IF reverse_getwordnum_walter("one, two, three") != "three, two, one"
>    ? "reverse_getwordnum_walter() fails test"
>ENDIF
>
>? ""
>FOR lnPass = 1 TO 5
>    ? "**** Pass " + TRANSFORM(lnPass)
>    lnStart = SECONDS()
>    FOR lnI = 1 TO 100000
>        k = reverse_alines_mdot("one, two, three")
>    NEXT
>    laAlinesM[lnPass] = SECONDS() - lnStart
>    ? "ALINES() m.", SECONDS() - lnStart
>
>    lnStart = SECONDS()
>    FOR lnI = 1 TO 100000
>        k = reverse_alines("one, two, three")
>    NEXT
>    laAlines[lnPass] = SECONDS() - lnStart
>    ? "ALINES()", SECONDS() - lnStart
>
>    lnStart = SECONDS()
>    FOR lnI = 1 TO 100000
>        k = reverse_getwordnum_rick("one, two, three")
>    NEXT
>    laGetWordNumR[lnPass] = SECONDS() - lnStart
>    ? "GETWORDNUM() Rick", SECONDS() - lnStart
>
>    lnStart = SECONDS()
>    FOR lnI = 1 TO 100000
>        k = reverse_getwordnum_walter("one, two, three")
>    NEXT
>    laGetWordNumW[lnPass] = SECONDS() - lnStart
>    ? "GETWORDNUM() Walter", SECONDS() - lnStart
>    ? ""
>NEXT
>
>? "**** Averages"
>? "ALINES() m.", show_average(@laAlinesM)
>? "ALINES()", show_average(@laAlines)
>? "GETWORDNUM() Rick", show_average(@laGetWordNumR)
>? "GETWORDNUM() Walter", show_average(@laGetWordNumW)
>
>
>FUNCTION show_average
>LPARAMETERS taArray
>EXTERNAL ARRAY taArray
>LOCAL lnI, lnAvg
>    lnAvg = 0
>    FOR lnI = 1 TO ALEN(taArray, 1)
>        lnAvg = lnAvg + taArray[lnI]
>    NEXT
>    RETURN ROUND(lnAvg / ALEN(taArray, 1), 2)
>
>
>FUNCTION reverse_alines_mdot
>LPARAMETERS tcText
>LOCAL lnLines, lcText, x
>LOCAL ARRAY laLines[1]
>    IF "," $ tcText
>        lnLines = ALINES(laLines,m.tcText,1,",")
>        lcText  = laLines[m.lnLines]
>        FOR x = lnLines-1 TO 1 STEP -1
>            lcText = m.lcText + ", " + laLines[m.x]
>        ENDFOR x
>        RETURN m.lcText
>    ENDIF
>    RETURN tcText
>
>
>FUNCTION reverse_alines
>LPARAMETERS tcText
>LOCAL lnLines, lcText, x
>LOCAL ARRAY laLines[1]
>    IF "," $ tcText
>        lnLines = ALINES(laLines, tcText, 1, ",")
>        lcText  = laLines[lnLines]
>        FOR x = lnLines-1 TO 1 STEP -1
>            lcText = lcText + ", " + laLines[x]
>        ENDFOR x
>        RETURN lcText
>    ENDIF
>    RETURN tcText
>
>
>FUNCTION reverse_getwordnum_rick
>LPARAMETERS tcString
>LOCAL lnI, lcString
>    IF "," $ tcString
>        * Reverse comma-delimited items
>        lcString = SPACE(0)
>        FOR lnI = GETWORDCOUNT(tcString, ",") TO 1 STEP -1
>            lcString = lcString + IIF(NOT EMPTY(lcString), ", ", SPACE(0)) + ALLTRIM(GETWORDNUM(tcString, lnI, ","))
>        NEXT
>    ELSE
>        * Pass-thru
>        lcString = tcString
>    ENDIF
>    RETURN lcString
>
>
>FUNCTION reverse_getwordnum_walter
>LPARAMETERS tcString
>LOCAL lcString
>    IF "," $ tcString
>        * Reverse comma-delimited items
>        lcString = ""
>        FOR nT =  GETWORDCOUNT(tcString, ",") TO 1 STEP -1
>            lcString = lcString + ALLTRIM(GETWORDNUM(tcString, nT, ",")) + ", "
>        ENDFOR
>        RETURN RTRIM(RTRIM(lcString), 1, ",")
>    ELSE
>        * Pass-thru
>        lcString = tcString
>    ENDIF
>    RETURN lcString
>
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform