Re: Anyone who uses (has used) strTok by Kat

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

John F Dutcher wrote:
> 
> 
> I have the luxury of a mainframe to analyze files I build or revise 
> in Euphoria for correctness. I tried "strTok" to parse a file, find two
> matching tokens received from an input screen....and make a change to
> one "field" in the parsed input, all the time "deparsing" and writing
> all records to a new output file. This is followed by uploading both
> files to the mainframe to compare large files with a utility.
> 
> Parse/deparse works so beautifully  ..  except where multiple commas
> (representing "missing" values) occur in the input. Here the 
> parse/deparse does not "hold" the consecutive commas when "deparsed"
> and they are lost upon output.
> 
> Has anyone dealt with this easily ?? 
> 
> Now that my sequence comparison education has been upgraded...I will
> re-try Derek's sub-routine for the same file exercise.
> 
Here is the parse() function in its barest form (strtok version is fancier):


global function parse(sequence s, integer c)
integer slen, spt, flag
sequence parsed

    parsed = {}
    slen = length(s)

    spt = 1
    flag = 0
    for i = 1 to slen do
        if s[i] = c then
            if flag = 1 then
                parsed = append(parsed,s[spt..i-1])
                flag = 0
                spt = i+1
            else
                spt += 1
            end if
        else
            flag = 1
        end if
    end for
    if flag = 1 then
        parsed = append(parsed,s[spt..slen])
    end if
    return parsed
end function

parse() does not perserve empty elements between delimiters.  Following is the
explode() function, which does:

global function explode(sequence s, integer c)
integer slen, spt, flag
sequence exploded
-- parse by delimiter, perserve blanks

    exploded = {}
    slen = length(s)

    spt = 1
    for i = 1 to slen do
        if s[i] = c then
            exploded = append(exploded,s[spt..i-1])
            spt = i+1
        end if
    end for
    exploded = append(exploded,s[spt..slen])

    return exploded
end function

Both of these assume that you are parsing strings with single-character
delimiters...

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

Search



Quick Links

User menu

Not signed in.

Misc Menu