in reply to Please help, (perl hash)

degit,

Sorry to repeat but it seems pieces of your answers are scattered across several replies.

Your Stated Requirements:

  1. Use a hash
  2. Track towns A-Z
  3. Track streets 1-9
  4. Track number of houses on each street
  5. Access number of houses as a combined key of Town and Street
  6. Street should be the primary key
  7. (Implied) how to loop through the streets and extract the other information

Please correct the assumptions. Otherwise, solution goes something like this:

#!/usr/bin/perl -w use strict; my %HouseInformation = (); { # Set up some information for testing $HouseInformation{6}{G} = 17; # 17 houses in Town G, Street +6 $HouseInformation{6}{A} = 9; # 9 houses in Town A, Street +6 $HouseInformation{8}{E} = 2; # 2 houses in Town E, Street +8 $HouseInformation{8}{K} = 8; # 8 houses in Town K, Street +8 $HouseInformation{9}{N} = 3; # 3 houses in Town N, Street +9 $HouseInformation{4}{J} = 7; # 7 houses in Town J, Street +4 # Loop through each street and get information foreach my $street (sort keys %HouseInformation) { foreach my $town (sort keys %{$HouseInformation{$street}}) { my $houseCount = $HouseInformation{$street}{$town}; print "Town $town, Street $street has $houseCount house +s.\n"; } } } exit;

Results:

C:\Steve\Projects\PerlMonks\TownStreetHouse>dir
 Volume in drive C is CBoot
 Volume Serial Number is B4AB-1A65

 Directory of C:\Steve\Projects\PerlMonks\TownStreetHouse

05/16/2011  09:19 AM    <DIR>          .
05/16/2011  09:19 AM    <DIR>          ..
05/16/2011  09:19 AM               934 TownStreetHouse.pl
               1 File(s)            934 bytes
               2 Dir(s)   3,312,336,896 bytes free

C:\Steve\Projects\PerlMonks\TownStreetHouse>perl TownStreetHouse.pl
Town J, Street 4 has 7 houses.
Town A, Street 6 has 9 houses.
Town G, Street 6 has 17 houses.
Town E, Street 8 has 2 houses.
Town K, Street 8 has 8 houses.
Town N, Street 9 has 3 houses.

C:\Steve\Projects\PerlMonks\TownStreetHouse>

Is that what you're looking for? As mentioned earlier, this is a Hash of Hashes (often abbreviated HoH or HOH). It may be helpful to think of it as a two-dimensional hash. Or as a two dimensional array where the index can be letters as well as numbers.

Good luck.