develooper Front page | perl.beginners | Postings from March 2002

Newbie Help please.

Thread Next
March 31, 2002 03:24
Newbie Help please.
Message ID:
Hi Everyone-

    I'm just starting to learn perl programing. I'm doing this through the
O'Reilly book:
Learning Perl.

I seem to be stuck on hashes. I was doing the exercise in the back of
chapter 5. This program wants a hash with 3 keys and 3 values. It then asks
for a string. That string is then matched to a value and the value is
printed. My understanding of the exercise was to define the hash with the
three pairs (keys, values). Then ask for a key and print the value This is
the program I wrote:

#!/usr/bin/perl -w
$map{"red"} = "apple";
$map{"green"} = "grass";
$map{"blue"} = "ocean";
print "A string please: "; chomp ($some_string = <STDIN>);
print "The value for $some_string is $map($some_string)\n";

When I run it though I get the following complaint from perl:

A string please: blue
Use of uninitialized value in concatenation (.) at ./ line 6, <STDIN>
line 1.
The value for blue is (blue)

I thought Ok I messed up so I tried the answer in the back of the book which
was the following:

#!/usr/bin/perl -w
%map = qw(red apple green leaves blue ocean);
print "A string please: "; chomp ($some_string = <STDIN>);
print "The value for $some_string is $map($some_string)\n";

This too gave me the same output from perl.
So here are my questions?

1.) What am I doing wrong here?
2.) Is the only difference between a hash and an array is that the array
uses predetermined numerical keys for each value?

Thanks in advance.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About