Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Reverse comma-delimited string
Message
From
04/09/2019 09:43:29
 
 
To
04/09/2019 06:26:27
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01670542
Message ID:
01670570
Views:
96
>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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform