RE: Replacing characters (Matt: bug)

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

> -----Original Message-----
> From: Dan Moyer [mailto:DANIELMOYER at prodigy.net]

> I've been using your useful replace_all function, & found 
> that if I happen
> to try to replace the last character on a line it errors.

Sure does.  Guess I never tried to do that.  Add the if statement around the
call to match() in the loop:

function replace_all( sequence text, object a, object b )
    integer ix, jx

    if atom(a) then
        a = {a}
    end if

    if atom(b) then
        b = {b}
    end if

    ix = 0
    jx = match( a, text )
    while jx do
        ix += jx
        text = text[1..ix-1] & b & text[ix+length(a)..length(text)]
        ix += length(b)

        if ix < length(text) then
            jx = match( a, text[ix+length(a)..length(text)] )
        else
            jx = 0
        end if
    end while

    return text
end function

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

Search



Quick Links

User menu

Not signed in.

Misc Menu