Click to See Complete Forum and Search --> : declaring local hash w/ Perl


moose
10-09-2000, 09:30 AM
Ok...so here is the code I have:


#!/usr/bin/perl -w
use strict;

@fruit{"red","green","blue"} = ("apple","leaves","ocean");
print "Pick a color (red, green or blue): ";
my $choice;
chomp ($choice = <STDIN> );
print "Your selection of $choice, maps to $fruit{$choice}\n";


So my questions are these:

1. How do I declare the hash? The script keeps exiting with this error:

"Global symbol "%fruit" requires explicit package name at ./hash1.pl line 5."

I know this has something to do with making this a local variable/array, but when I try:


my @fruit{"red","green","blue"} = ("apple","leaves","ocean");
....


I get the error:

Can't declare hash slice in my at ./hash1.pl line 5, near "} ="


So basically, I know....er...I think I know what Perl wants me to do...I'm just not sure how to.

Second...

In the second part of the script


my $choice;
chomp ($choice = <STDIN> );


This works...but it seems like there would be a better way to do this. Is there a way to combine the two lines? Or should I get into the habit of writing like this...with the declaration first...followed by whatever code.

I know these are very easy questions...but hey...we all have to start somewhere! http://www.linuxnewbie.org/ubb/smile.gif

Thanks for helpin'

------------------
Situation normal.
Everything is all fsckd up!

jemfinch
10-09-2000, 10:43 AM
my %hash = (
red => 'apple',
green => 'leaves',
blue => 'ocean',
);


Jeremy