in reply to hash of hashes

I know that it can be done with a hash of arrays (which I also was having trouble with), but I want to do it with a hash of hashes, if possible

Why on earth would you want to do that? For each letter sequence you want the list of numbers that were associated with it. That shouts for a hash of arrays.

In the loop, when you have $lets and $nums, collect the numbers belonging to $lets:

push @{ $plates{ $lets} }, $nums;
To print the results:
print "$_ ", join( ', ', @{ $plates{ $_}), "\n" for sort keys %plates;
(Code untested)

Anno

Replies are listed 'Best First'.
Re^2: hash of hashes
by Joost (Canon) on Mar 23, 2007 at 21:46 UTC
    Why on earth would you want to do that?
    You'd want to do that in perl if the original input contains duplicates and you only want to show unique license plate numbers. Since perl doesn't have a built-in concept of a (uniqe) set hashes are the natural structure to use.

    To the OP: note that i use sort here, but that's just because I presume you'd want the output to be predictable.

    $plates{$lets}{$number} =1; # or $plates{$lets}{$number}++ if you wan +t to count # and for my $let (sort keys %plates) { print "$let ",join(", ",sort keys %{$plates{$let}}),"\n"; }
    Updated: removed map() - code should now be correct
      Hi Joost,

      You are correct that I am using a hash of hashes because of duplicates. The program will be reading multiple files, many of which will contain the same license plate.

      I'm going to try your code now and will report back shortly.

      Many thanks! 8^}
      Well, the OP said

      proper output would look like:

      ABC 123, 334, 442 FTE 442 HHR 443 NTR 554, 554

      Note the last line, which shows a duplicate number.

      I have seen the OPs reply that confirms that duplicates are indeed unwanted. The sample output seemed to indicate the opposite.

      Anno

        Putting a duplicate in there was a mistake, so you were quite right in thinking a hash of a hash would be a bad idea since it looked like duplicates were allowed. Thanks for your help.