Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Re: Parsing Challenge

by Albannach (Monsignor)
on May 24, 2001 at 23:14 UTC ( #83087=note: print w/replies, xml ) Need Help??

in reply to Re: Parsing Challenge
in thread Parsing Challenge

Though we have arrived at very similar solutions, I think yours fails to pick up multiple values for the last key:
use strict; use warnings; use Data::Dumper; my $data="key1=value1 key2=value2 value3 key3=value4 value5 value6 key +4=value7 key5=value8 key6=value9 value10"; # Albannach version my %hash; while ($data =~ /(\w+=)((\w+( |$)+)+)/g) { $hash{$1} = $2; } print Dumper \%hash; # gumpu version: my %foo; while ($data =~ m/(\S+)=(\S+($|([^=]+\s+)+))/g) { $foo{$1}=$2; } print Dumper \%foo;
$VAR1 = { 'key4=' => 'value7 ', 'key5=' => 'value8 ', 'key1=' => 'value1 ', 'key6=' => 'value9 value10', 'key2=' => 'value2 value3 ', 'key3=' => 'value4 value5 value6 ' }; $VAR1 = { 'key1' => 'value1 ', 'key2' => 'value2 value3 ', 'key3' => 'value4 value5 value6 ', 'key4' => 'value7 ', 'key5' => 'value8 ', 'key6' => 'value9 ' };

Update: The = in the key? You are right of course - silly me! Thanks gumpu!

I'd like to be able to assign to an luser

Replies are listed 'Best First'.
Re: Re: Re: Parsing Challenge
by gumpu (Friar) on May 25, 2001 at 10:36 UTC

    It does not pick up the last value for the last key, should have tested it better. I also think that your version is more readable. Though I would leave out the = from the key.

    $hash{$1} = $2 while ($data =~ /(\w+)=((\w+( |$)+)+)/g);

    Have Fun

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2023-01-27 21:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found