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);