D'oh! Late waking up this morning. That's because hashes can only have unique keys... you can create a hash of arrays to fix this issue:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %h; my @a; while (<DATA>){ chomp; my @a = split /@@@/; push @{ $h{$a[0]} }, $a[1]; } print Dumper \%h __DATA__ CustomerA@@@DomainA CustomerA@@@DomainB CustomerA@@@DomainC CustomerA@@@DomainD CustomerA@@@DomainE CustomerB@@@DomainA CustomerB@@@DomainB CustomerB@@@DomainC CustomerB@@@DomainD CustomerB@@@DomainE __END__ $VAR1 = { 'CustomerB' => [ 'DomainA', 'DomainB', 'DomainC', 'DomainD', 'DomainE' ], 'CustomerA' => [ 'DomainA', 'DomainB', 'DomainC', 'DomainD', 'DomainE' ] };
Then access everything like this:
for my $key (keys %h){ for my $elem (@{ %h{$key} }){ print "$key: $elem\n"; } }
In reply to Re^3: Filling a Hash
by stevieb
in thread Filling a Hash
by iRemix94
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |