Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: print identical keys once along with their values

by Laurent_R (Canon)
on Nov 04, 2015 at 22:02 UTC ( [id://1146943]=note: print w/replies, xml ) Need Help??


in reply to print identical keys once along with their values

Actually, if you only want to output the data the way you've shown, then you could do it without nested data structure and use a simple hash (or a simple array), provided you concatenate the values into the hash as you go (re-using part of AnomalousMonk's code):
$ perl -wMstrict -MData::Dumper -e 'my $pair = qr{ (\d+) \s+ ([[:alpha +:]]+) }xms; > my $record = "1 A 1 B 2 G 2 H 2 V"; > my %hash; > while ($record =~ m{ \G \s* $pair }xmsg) { > $hash{$1} .= "$2;"; > } > print Dumper \%hash; > ' $VAR1 = { '1' => 'A;B;', '2' => 'G;H;V;' };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found