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.


In reply to Re: Please help, (perl hash) by marinersk
in thread Please help, (perl hash) by degit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.