declaring local hash w/ Perl


Results 1 to 2 of 2

Thread: declaring local hash w/ Perl

  1. #1
    moose Guest

    declaring local hash w/ Perl

    Ok...so here is the code I have:

    Code:
    #!/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:

    Code:
    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

    Code:
    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!

    Thanks for helpin'

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

  2. #2
    jemfinch Guest
    Code:
    my %hash = (
    	red	=>	'apple',
    	green	=>	'leaves',
    	blue	=>	'ocean',
    );
    Jeremy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •