Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: merge with multiple row with same key

by Cristoforo (Curate)
on Nov 27, 2014 at 03:59 UTC ( [id://1108528]=note: print w/replies, xml ) Need Help??


in reply to merge with multiple row with same key

Probably a better thought out solution could be made, but I think this may be what you're after.
#!/usr/bin/perl use strict; use warnings; my %hash = ( 'abc,def' => { ',,,item4' => 1, 'item1,,,' => 1, ',item2,,' => 1 }, 'kln,mno' => { ',,,item4' => 1, 'item1,,,' => 1 }, ); my %new_hash; for my $key (keys %hash) { my @array; for (keys %{ $hash{$key}}) { my ($lead_comma,$item) = /^(,*)([^,]+)/; $array[ length $lead_comma ] = $item; } $new_hash{$key} = \@array; } use Data::Dumper; print Dumper \%new_hash; __END__ output from Data::Dumper $VAR1 = { 'abc,def' => [ 'item1', 'item2', undef, 'item4' ], 'kln,mno' => [ 'item1', undef, undef, 'item4' ] };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found