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

Re: Regex to capture field values

by Anonymous Monk
on Jan 13, 2016 at 13:32 UTC ( #1152673=note: print w/replies, xml ) Need Help??


in reply to Regex to capture field values

functional style:
use strict; use warnings; use Data::Dumper; use List::Util 'reduce'; my @array = ( 'name riya', 'age 34', 'company xxxx', 'name vinoth', 'age 25', 'call 12345678', 'company xxxx', 'address asdd', 'country Ind' ); my $hashes = reduce { my ( $key, $val ) = split / /, $b; push @$a, {} if $key eq 'name'; $a->[-1]{$key} = $val; $a; } [], @array; print Dumper $hashes;
output:
$VAR1 = [ { 'age' => '34', 'company' => 'xxxx', 'name' => 'riya' }, { 'call' => '12345678', 'address' => 'asdd', 'name' => 'vinoth', 'country' => 'Ind', 'company' => 'xxxx', 'age' => '25' } ];

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2020-09-20 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (120 votes). Check out past polls.

    Notices?