Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Can a hash name have the same value twice.

( #9968=categorized question: print w/ replies, xml ) Need Help??
Contributed by DavidBer on May 02, 2000 at 23:02 UTC
Q&A  > hashes


I am trying to read in a file. The structure of the file is city:country. What I need to do is to show cities within a country. My thought was to read the file into a hash, with it like this:

US:New York

I understand that an @ array basically uses a number as the identifier of the value. How does a hash do it?

Answer: Can a hash name have the same value twice.
contributed by reptile

There is a much better answer in this case than storing multiple bits of information in the hash keys. I would personally never do that unless I absolutely had to. The example given in this question is actually a really simple data structure: One name to Multiple values, or in perl, a hash whose values are array references. Enough lecturing now.

my %countries = (); my $foo = "US:New York"; my ($country, $city) = (split /:/, $foo); push @{ $countries{$country} }, $city;

This code assumes you understand references in perl.

Then to print all the cities within the country:

foreach my $city (@{$countries{$country}}) { print "$city\n"; }

It's trivial to include even more information than that as well; turn that array reference into a hash reference by city, containing maybe another hash reference with even more information or an array with a list of people who live there.

Answer: Can a hash name have the same value twice.
contributed by chromatic

If you need to store more information, consider a more complex data structure. reptile suggests a hash of lists. I would choose a hash of hashes.

my %data = ( 'US' => { Florida => 'Miami', Illinois => 'Chicago', New York => 'New York City' }, 'Canada' => { British Columbia => 'Alberta', Saskatchewan => 'Saskatoon', Ontario => 'Toronto' } );
The appropriate perldoc pages are perldsc and perlref.

(Oh, a hash is an associative array. That means that it takes the first element -- New York, for example -- and runs it through a hashing algorithm to generate a unique index, which helps it locate the key -- New York City -- in an internal data structure. If the hashing algorithm is good, "New York" will always come up with the same hash value, which is why duplicate keys are not very useful.)

Answer: Can a hash name have the same value twice.
contributed by Roger

Create a two dimensional hash like this:

@cities = qw/ US:Miami AU:Melbourne US:Chicago /; map {my($co,$ci)=split/:/,$_; $n{$co}{$ci}=1 } @cities; foreach $x (sort keys %n) { foreach (sort keys %{$n{$x}}) { printf "$x - $_\n"; } }
Which will print out:

AU - Melbourne
US - Chicago
US - Miami
Answer: Can a hash name have the same value twice.
contributed by turnstep

Here is a simple way to do it:

open(MYFILE, "$cityfile") || die "Could not open $cityfile: $!\n"; while(<MYFILE>) { chomp; ($city, $country) = split(/:/, $_); $place{$country} .= "$city#"; } close(MYFILE); for $x (keys %place) { print "$x:\n"; for (split(/#/, $place{$x})) { print "\t$_\n"; } }
Answer: Can a hash name have the same value twice.
contributed by Roger

This is to amend my previous post, the foreach loop can certainly be rewritten with a one liner:

map {my $key=$_;map {print "$key - $_\n"} sort keys %{$n{$_}} } sort keys %n;
Answer: Can a hash name have the same value twice.
contributed by Bloodelf

If you want to put all of the the values for each key together, then give this a go.(I'm Assuming the file is in the above format.)

open (FILE,"c:/file.txt"); %hash=(); while (<FILE>) {chomp; split /:/; if ($hash{$_[0]}){$hash{$_[0]}.=",$_[1]";} else {$hash{$_[0]}=$_[1];} }

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (3)
    As of 2016-02-13 19:35 GMT
    Find Nodes?
      Voting Booth?

      How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

      Results (443 votes), past polls