Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Reverse comma-delimited string
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01670542
Message ID:
01670591
Vues:
99
J'aime (1)
>I have a string like "one, two, three" and I need it to be in the order "three, two, one".
>
>Does anybody have an efficient algorithm to do this?

Just for fun as I will definitely use alines, I wrote this which of course is much slower than alines, but much faster than getwordnum using rat()

(below is all the code, including the reverse_ratstring() function)
CLEAR
DIMENSION laAlinesM[5]
DIMENSION laAlines[5]
DIMENSION laGetWordNumR[5]
DIMENSION laGetWordNumW[5]
dimension laratstring[5]

DIMENSION laUnique[500]
FOR lnI = 1 TO ALEN(laUnique, 1)
    laUnique[lnI] = SYS(2015)
NEXT

? ""
FOR lnPass = 1 TO 5

    * Build strings for this iteration
    lcReverse   = SPACE(0)
    lcForward   = SPACE(0)
    lnEnd       = lnPass * 100
    FOR lnI = 1 TO lnEnd
        lcForward = lcForward + laUnique[lnI] + ", "
        lcReverse = lcReverse + laUnique[lnEnd - lnI + 1] + ", "
    NEXT
    lcForward = RTRIM(RTRIM(lcForward), 1, ",")
    lcReverse = RTRIM(RTRIM(lcReverse), 1, ",")

    IF reverse_alines_mdot(lcForward) != lcReverse
        ? "reverse_alines_mdot() fails test on pass " + TRANSFORM(lnPass)
    ENDIF
    IF reverse_alines(lcForward) != lcReverse
        ? "reverse_alines() fails test on pass " + TRANSFORM(lnPass)
    ENDIF
    IF reverse_getwordnum_rick(lcForward) != lcReverse
        ? "reverse_getwordnum_rick() fails test on pass " + TRANSFORM(lnPass)
    ENDIF
    IF reverse_getwordnum_walter(lcForward) != lcReverse
        ? "reverse_getwordnum_walter() fails test on pass " + TRANSFORM(lnPass)
    endif
    lcCalcReverse = reverse_ratstring(lcForward)
    IF lcCalcReverse != lcReverse
    	set step on 
        ? "reverse_ratstring() fails test on pass " + TRANSFORM(lnPass)
    ENDIF

    ? "**** Pass " + TRANSFORM(lnPass) + ", using " + TRANSFORM(lnPass * 100) + " elements"
    lnStart = SECONDS()
    FOR lnI = 1 TO 1000
        k = reverse_alines_mdot(lcForward)
    NEXT
    laAlinesM[lnPass] = SECONDS() - lnStart
    ? "ALINES() m.", SECONDS() - lnStart

    lnStart = SECONDS()
    FOR lnI = 1 TO 1000
        k = reverse_alines(lcForward)
    NEXT
    laAlines[lnPass] = SECONDS() - lnStart
    ? "ALINES()", SECONDS() - lnStart

    lnStart = SECONDS()
    FOR lnI = 1 TO 1000
        k = reverse_getwordnum_rick(lcForward)
    NEXT
    laGetWordNumR[lnPass] = SECONDS() - lnStart
    ? "GETWORDNUM() Rick", SECONDS() - lnStart

    lnStart = SECONDS()
    FOR lnI = 1 TO 1000
        k = reverse_getwordnum_walter(lcForward)
    NEXT
    laGetWordNumW[lnPass] = SECONDS() - lnStart
    ? "GETWORDNUM() Walter", SECONDS() - lnStart

    lnStart = SECONDS()
    FOR lnI = 1 TO 1000
        k = reverse_ratstring(lcForward)
    NEXT
    laratstring[lnPass] = SECONDS() - lnStart
    ? "ratstring()", SECONDS() - lnStart
    ? ""
NEXT

? "**** Averages"
? "ALINES() m.", show_average(@laAlinesM)
? "ALINES()", show_average(@laAlines)
? "GETWORDNUM() Rick", show_average(@laGetWordNumR)
? "GETWORDNUM() Walter", show_average(@laGetWordNumW)
? "ratstring()", show_average(@laratstring)


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_ratstring
LPARAMETERS tcText
local lcText, lnItem, lnPosition, lnLastPosition
	lcText = ''
	lnItem = 1
    lnPosition = RAT(',', m.tcText, m.lnItem)
    lnLastPosition = len(m.tcText)
    do while m.lnPosition > 0
    	lnItem = m.lnItem + 1
        lcText  = m.lcText + "," + SUBSTR(m.tcText, m.lnPosition + 1, m.lnLastPosition - m.lnPosition)
        lnLastPosition = m.lnPosition - 1
        lnPosition = RAT(',', m.tcText, m.lnItem)
    enddo
    return  substr(m.lcText, 3) + IIF(EMPTY(m.lcText), "", ", ") + substr(m.tcText, 1, m.lnLastPosition)

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
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform