Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Here’s one way to do it:
my %last_name = qw{
fred flintstone
barney rubble
wilma flintstone
};
print "Please enter a first name: ";
chomp(my $name = <STDIN>);
print "That's $name $last_name{$name}.\n";
In this one, we used a qw//
list (with curly braces as the delimiter) to initialize the hash.
That’s fine for this simple data set, and it’s easy to maintain
because each data item is a simple given name and simple family
name, with nothing tricky. But if your data might contain spaces—for
example, if robert de niro or mary
kay place were to visit Bedrock—this simple method
wouldn’t work so well.
You might have chosen to assign each key/value pair separately, something like this: