Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: extracting the key value pairs from a string

by johngg (Canon)
on Jun 27, 2008 at 08:56 UTC ( [id://694341]=note: print w/replies, xml ) Need Help??


in reply to Re: extracting the key value pairs from a string
in thread extracting the key value pairs from a string

split /[^:]\s+/, $s

That's not going to do what you hoped as it will consume the last character of each value except the one at the end of the string. You can get around that by using a negative look-behind. I also use map to avoid the for loop and temporary variables.

use strict; use warnings; use Data::Dumper; my $s = q{ ABC: 123 xyz: 100 def: YYY aaa: ZZZ}; my %extract = map { split m{\s*:\s*} } split m{(?<!:)\s+}, $s; print Data::Dumper->Dumpxs( [ \ %extract ], [ q{*extract} ] );

This produces

%extract = ( 'ABC' => '123', 'def' => 'YYY', 'aaa' => 'ZZZ', 'xyz' => '100' );

I hope this is of interest.

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found