Re: piece function - there must be an easier/more elegant way

new topic     » topic index » view thread      » older message » newer message

Hello Derek,

Thanks for this I'll give it a test when I've some "quality coding time" :-]

On first glance I really like the:

    -- Ensure the string always ends with a delimiter
    s = append(s,delimiter)

This will make testing for some of the more unusual boundary cases either
easier or not even necessary.  You've gotton me thinking now!

Also thanks "PatRat" for the "token" link - again I'll take a look at that
very soon.

Regards,

FP.

At 12:52 AM 5/15/01 +1000, you wrote:
>I don't know about "more elegant" but here's anither attempt...
>
>--------------------
>function piece(sequence s, integer delimiter, integer position)
>
>    integer  startat, endat
>
>    -- Ensure the string always ends with a delimiter
>    s = append(s,delimiter)
>
>    -- initialize
>    startat = 1
>    endat = 1
>
>    -- look at each substring
>    while position > 0 do
>
>        -- find the end of the current substring
>        endat = find(delimiter, s)
>
>        -- If it doesn't end, then return nothing
>        if endat = 0 then
>                return ""
>        end if
>
>        -- remove the delimiter
>        s[endat] = delimiter + 1
>
>        -- indicate I've finished with this substring
>        position -= 1
>
>        -- if there are any more to scan for, updated the start locn.
>        if position > 0 then
>            startat = endat + 1
>        end if
>    end while
>
>    -- return the substring
>    return s[startat .. endat - 1]
>end function
>-----------------
>Derek Parnell
>Melbourne, Australia
>"To finish a job quickly, go slower."
>
>----- Original Message -----
>From: <freeplay at mailandnews.com>
>To: "EUforum" <EUforum at topica.com>
>Sent: Monday, May 14, 2001 10:55 PM
>Subject: piece function - there must be an easier/more elegant way !?
<snip>

>
>
>

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu