Re: field extraction ala awk

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

I think keyvalues may suit your needs better. You can use it to chop up key/value pairs or simple delimited lists, where you need to account for quotes, white space, etc. It's really quite versatile!

Here's a contrived example I threw together:

 
include std/io.e 
include std/text.e 
include std/types.e 
include std/utils.e 
 
procedure print_pairs( integer fn, sequence data, integer haskeys = 1 ) 
 
    -- do not use white space as a delimiter for data with keys 
    sequence whitespace = iff( haskeys, "", " \t\r\n" ) 
    sequence pairs = keyvalues( data,,,, whitespace, haskeys ) 
 
    for i = 1 to length( pairs ) do 
 
        if haskeys then 
            sequence key = pairs[i][1] 
            sequence value = pairs[i][2] 
            printf( fn, "%s = %s\n", {key,value} ) 
        else 
            printf( fn, "%d: %s\n", {i,pairs[i]} ) 
        end if 
 
    end for 
 
end procedure 
 
print_pairs( STDOUT, "name=Homer Simpson; address=742 Evergreen Tr; city=Springfield; state=OR; country=USA;" ) 
 
-- name = Homer Simpson 
-- address = 742 Evergreen Tr 
-- city = Springfield 
-- state = OR 
-- country = USA 
 
print_pairs( STDOUT, "Homer Marge Bart Lisa Maggie Grampa \"Santa's Little Helper\" \"Snowball II\"", FALSE ) 
 
-- 1: Homer 
-- 2: Marge 
-- 3: Bart 
-- 4: Lisa 
-- 5: Maggie 
-- 6: Grampa 
-- 7: Santa's Little Helper 
-- 8: Snowball II 
 

-Greg

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

Search



Quick Links

User menu

Not signed in.

Misc Menu