in reply to Parsing Challenge
How about this:
#!/usr/bin/perl -w ############################################################### use strict; use Data::Dumper; my $data=qq(key1=value1 key2=value2 key3=value3 key4=value4); my $v={}; foreach my $pair (split(' ',$data)) { my($key,$value)=split('=',$pair); $v->{$key}=$value; } print Dumper($v);
Yields:
$VAR1 = { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4' };
Alternatively you can do the following with the same results
#!/usr/bin/perl -w ############################################################### use strict; use Data::Dumper; my $data=qq(key1=value1 key2=value2 key3=value3 key4=value4); my $v={}; map { my($k,$vl) = split('=',$_); $v->{$k}=$vl; } split(' ',$data); print Dumper($v);
HTH
| Peter L. Berghold | Schooner Technology Consulting, Inc. |
| Peter@Berghold.Net | www.berghold.net |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Parsing Challenge
by no_slogan (Deacon) on May 24, 2001 at 22:37 UTC | |
by blue_cowdawg (Monsignor) on May 24, 2001 at 22:39 UTC |