Robert Canary wrote:
> 
> Hi,
> 
> Sorry for the perl question.....
> 
> I have extracted from a log file ta line that reads that reads something
> like this:
> item1='value', item2='value2', item3='value3'
> 
> i am trying to get parse the line as to assign to an array as such:
> $line{item1}=$value1 etc etc etc
> 
> I can't figure out how to get the line into an array.   Can anyone offer
> some ideas?

You may need to adjust the parsing line a little depending on what is
valid for your keys and values, but this should work otherwise.   Just
tested it here.  Next time, though, check out the newsgroup
comp.lang.perl.  If you don't have news access where you are, you can
use http://www.deja.com.


open(IN,'foo') or die("$0: Could not open foo for read: $1");

#Read file, load array
while(<IN>)
{
        chomp();
        $_ =~ /^(\w+)=(\w+)$/;
        if($1 ne '')
        {
                $Line{$1}=$2;
        }
}

foreach $Key (keys(%Line)) 
{
        print("[$Key]=[$Line{$Key}]\n");
}


If you would rather set a regular variable to the value instead of an
array element, change
                $Line{$1}=$2;
to 
                ${$1}=$2

-- 
-------------------------------------------------------------------
DDDD   David Kramer                    [EMAIL PROTECTED]
DK KD                                 http://kramer.ne.mediaone.net
DKK D  
DK KD  HYDROGEN: A colorless, odorless gas which,
DDDD   given enough time, turns into people.


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

Reply via email to