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

Re: Newbie Help please.

Thread Previous
From:
Billie
Date:
March 31, 2002 04:11
Subject:
Re: Newbie Help please.
Message ID:
20020331121139.12988.qmail@onion.perl.org
Hi

The term $map($some_string) actually means 2 scalar variables, one is $map
and the other is $some_string. You get warning of uninitialized  variable
because $map is never declared before.
The correct way to refer to a value in hash is $map{$some_string}. I think
there is a typo in the book

Hope this  help

Billie


"Sa" <sarmstrong13@mac.com> ???????:B8CBDC21.4631%sarmstrong13@mac.com...
> 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 ./key3.pl 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.
> SA
>



Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About