Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: basic hash question

by myocom (Deacon)
on Jun 23, 2001 at 02:44 UTC ( [id://90890]=note: print w/replies, xml ) Need Help??


in reply to Re: basic hash question
in thread basic hash question

No, it's not bad at all, as long as you remember that you need to quote those keys later (i.e., $hash{'two words'} works, where $hash{two words} does not).

As a somewhat overly contrived example, say you have a file with the rosters of each MLB team. Further, let's say it contains rosters for the last five seasons. Obviously many players are going to show up more than once. So how do you find out how many unique players there were over the last five years? Read the file and use the names as hash keys.

# Untested and unoptimized code use strict; my $filename = 'mlbroster.txt'; my %seen; open (FILE, $filename) or die "Couldn't open $filename: $!\n"; while (<FILE>) { chomp; $seen{$_}=1; # This will include, say, $seen{'Sammy Sosa'} } close(FILE); print "Unique players:\n", join("\n", sort keys %seen);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://90890]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found