in reply to Filling a Hash

You need to escape the @ symbol in a regex: /\@{3}/</strike>.

Hadn't had coffee yet... was thinking string interpolation.

Replies are listed 'Best First'.
Re^2: Filling a Hash
by iRemix94 (Sexton) on Jul 17, 2015 at 13:51 UTC

    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!

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