Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Reverse comma-delimited string
Message
From
03/09/2019 16:07:55
 
 
To
03/09/2019 14:29:41
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01670542
Message ID:
01670548
Views:
71
>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?

This got me thinking, for grins and giggles, does anybody have an INEFFICIENT or OBTUSE algorithm to do this?
lnStart = SECONDS()
FOR lnI = 1 TO 1000000
    k = reverse("one, two, three")
NEXT
? SECONDS() - lnStart


FUNCTION reverse
LPARAMETERS tcString
LOCAL lnI, lcString, lnLastFound, lcOutput
    IF NOT EMPTY(tcString)
        RETURN tcString
    ENDIF

    CREATE CURSOR c_temp ;
    ( ;
        cItem   m ;
    )

    lcString = "one, two, three"
    lnLastFound = 1
    FOR lnI = 1 TO LEN(lcString)
        IF SUBSTR(lcString, lnI, 1) = "," OR lnI = LEN(lcString)
            INSERT INTO c_temp (cItem) VALUES (SUBSTR(lcString, lnLastFound, lnI - lnLastFound + IIF(lnI = LEN(lcString), 1, 0)))
            lnLastFound = lnI + 1
        ENDIF
    NEXT

    lcOutput = SPACE(0)
    DO WHILE NOT BOF()
        lcOutput = lcOutput + IIF(NOT EMPTY(lcOutput), ", ", SPACE(0)) + cItem
        SKIP -1
    ENDDO
    RETURN lcOutput
:-)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform