in reply to Looping through a hash where some keys are the same

A hash has only unique keys. You might better use a array.
my @array = ( [ bob => "section-a"], [ bob => "section-b"] ); for ( @array ) { my ( $a, $b ) = @$_; print "$a => $b\n"; }
Or use Tie::DxHash this tied hash allows duplicate keys.
Boris

Replies are listed 'Best First'.
Re^2: Looping through a hash where some keys are the same
by atcroft (Abbot) on Feb 18, 2005 at 01:40 UTC

    Another option would be a hash of arrays, so that each key was unique, but referred to an array of one or more values:

    my %hash = ( 'bob' => [ "section-a", "section-b" ], 'jan' => [ "section-d" ] ); # And to add another.... foreach (qw(section-f section-g)) { push(@{$hash{'paul'}}, $_); } foreach my $k (keys(%hash)) { print "$k => ", join(",", @{$hash{$k}}), "\n"; }

    HTH.

      I'll look into those options. As for what the objective is... it's a script that scours an auction site I frequent b/c their alert system sucks so bad. My hash looks like

      "searchstring" => "section",

      There are some (very few) items that I would want to search for in different sub sections. For now, they're all in the same main section, so I just search that, but it gets like 300 hits or so each day... about half of it crap I don't want to see.

      I assume the smartest approach would to make the keys of the hash the section to search, and the value an array holding the different strings I want to search in that section, no? It'd take a fair bit of rewriting. Not sure if it's worth it at this point, but would be do-able.
        The value could be an arrayref, as you say, or it could be a hashref. Advantage of a hash ref is you don't have to check for duplicates, since duplicates aren't allowed in a hash. Personally I use hashes a lot more than arrays.