Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Parsing Challenge

by symŽ (Acolyte)
on May 25, 2001 at 00:33 UTC ( [id://83120]=note: print w/replies, xml ) Need Help??


in reply to Parsing Challenge

All great ideas, thanks! I tried many of them and settled on a hybrid. I had to make a few somewhat ugly tweaks because some of the key names were repeated with different values (intended to be different keys with the same name) funky.. but I just tacked on a increment number to the key name. Hey whatever works right?
Here is the code
my $data="fruit=pear meat=chicken legs fruit=orange slices cheese=mont +erey jack meal=lunch meat=ribs bread= "; my %foo; while ($data =~ m/(\S+)=(\S*($|([^=]+\s+)*))/g) { my ($tmpkey,$tmpval); my $count=1; $tmpkey=$1; $tmpval=$2; while (defined $foo{$tmpkey}){ $count++; $tmpkey=~s/\_\d+$//; $tmpkey.="_$count"; } $foo{$tmpkey}=$tmpval; } for (sort (keys %foo)){ if ($_!~/_\d+/){ my $rm=$_; $_.="_1"; $foo{$_}=$foo{$rm}; delete $foo{$rm}; } chomp ($_,$foo{$_}); print "$_->$foo{$_}\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://83120]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found