On Mon, 21 Feb 2000, Bret Hughes wrote:

> Got to something worng with this code.  Are you sure it's perl?  I mean,
> I can understand it.  too easy.  I wrote a perl script to parse a bunch
> of stuff from debug logs in a long running program about a year ago
> (thanks to O'riley and this list) and when I went back a couple of weeks
> ago I had no @&* idea what I had done.  Really powerful really cryptic. 
> Like I said earlier are you sure this is perl?
> 
> :) 
> > ================
> > 
> > open(IN, logfile) ;
> > 
> > while(<IN>) {
> >         foreach (split) {
> >                 ($key, $value) = split("=") ;
> >                 print "$key, $value\n" ;
> >         }
> > }
> > close IN ;
> > 
> > ================
> > 
> >   once you get the values in $key and $value, do with them whatever
> > you wish.

perhaps i wasn't solving the problem as it was asked, but what the above
does is read a file of lines, each line of the format

        key=value  key=value ....

and an arbitrary number of those lines.  a running commentary:

while (<IN>) {
        
        read the next line into the magic variable $_, no need to chomp,
        since we're going to deal with whitespace shortly anyway

foreach (split) {

        split the (default) magic variable $_, based on (default)
        multiple whitespace (a great trick).  putting this into a
        foreach loop places each "key=value" string into (tada!)
        the (local to this foreach loop) $_ magic variable again.

($key, $value) = split("=") ;

        split the "key=value" string in (default) $_ based on the
        delimiter being an equal sign, place in scalar variables
        $key and $value.  you can then print out these two values,
        put them in a hash, whatever.

any questions?

rday

p.s.  yeah, it's perl. :-)


-- 
To unsubscribe: mail [EMAIL PROTECTED] with "unsubscribe"
as the Subject.

Reply via email to