in reply to Appending values to existing hash key
A number of minor things, most of which are personal preference:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash; while (<DATA>) { (my $key, my $value) = /(\S+\s+\S+)\s+(\S+\s+\S+)/; $value = join ":", split " ", $value; $hash{$key} .= ", " if exists $hash{$key}; $hash{$key} .= $value; } print "$_ $hash{$_}\n" for sort keys %hash; __DATA__ id1 name1 cat1 catname1 id1 name1 cat2 catname2 id2 name2 cat3 catname3 id3 name3 cat1 catname1 id3 name3 cat4 catname4
Prints:
id1 name1 cat1:catname1, cat2:catname2 id2 name2 cat3:catname3 id3 name3 cat1:catname1, cat4:catname4
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Appending values to existing hash key
by jaypal (Beadle) on Mar 21, 2014 at 03:34 UTC |