concatenate hash values when key is same in perl -


i have hash :

abc=>1 hello=>32 abc=>4 hello=>23 hello=>12 xyz=>18 

how can concatenate values, keys same. output be:

abc=>"1,4" hello=>"23,12,32" xyz=>"18". 

i tried sorting hash keys checking each key, if same concatenate value, not getting how compare 2 keys in same loop.

in advance.

the exact way works depends on real source of data, program shows way read information data filehandle build , dump hash.

the values of hash anonymous arrays contain values corresponding same key.

use strict; use warnings;  %data;  while (<data>) {   ($k, $v) = /\w+/g;   push @{ $data{$k} }, $v; }  $k (sort keys %data) {   printf "%s => %s\n", $k, join ',', @{ $data{$k} }; }  __data__ abc=>1 hello=>32 abc=>4 hello=>23 hello=>12 xyz=>18 

output

abc => 1,4 hello => 32,23,12 xyz => 18 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -