in reply to Re: Filling a Hash
in thread Filling a Hash

oh well, the splitting part works as you see. The problem is the overwritten hash in this case. But your tip also makes my code better, thanks for that!

Replies are listed 'Best First'.
Re^3: Filling a Hash
by stevieb (Canon) on Jul 17, 2015 at 14:09 UTC

    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"; } }