JustLikeThat has asked for the wisdom of the Perl Monks concerning the following question:

Hello! I just started to learn perl a few weeks ago using the O'Reilly books so please bare with this simple question.

I am using Visual Studio (with the Perl mod from activestate) to write this code to work in windows.

The following code is a piece of a larger program. I want to ask the user for a number and use that as the key to pull a value out of the hash. But I have not been successful. Her is the code below:

#!/usr/bin/perl print "Give me a number for the tile and I will tell you the color\n"; $a=<STDIN>; print $a."\n"; %Number = qw(1 Red 2 Yellow 3 Green 4 Black 5 Purple 6 Pink 7 Brown 8 Blue 9 Orange); print $Number{$a}."\n";
The strange thing is when I set $a to a value in the code (ie $a=5 instead of <STDIN>) I get the correct answer. Any ideas?!! And thanks in advance for your help!!

Replies are listed 'Best First'.
Re: Selecting a specific hash key to find a value
by zejames (Hermit) on Sep 23, 2004 at 16:57 UTC
    Your have to chomp the variable, that is remove the trailing \n :

    #!/usr/bin/perl print "Give me a number for the tile and I will tell you the color\n"; chomp($a=<STDIN>); print $a."\n"; %Number = qw(1 Red 2 Yellow 3 Green 4 Black 5 Purple 6 Pink 7 Brown 8 Blue 9 Orange); print $Number{$a}."\n";

    BTW, your code will be cleaner if you construct your hash like this :

    %Number = (1 => 'Red', 2 => 'Yellow', 3 => 'Green');

    It'll be clearer that you are using a hash table.

    On the other way you could use a array for that :

    @Number = ('Red', 'Yellow', 'Green'); print $Number[$a]."\n";

    That is much more logical, as your keys are consecutive numbers. Hash a usually used when keys are strings or more complicated objects.

    --
    zejames
Re: Selecting a specific hash key to find a value
by Sidhekin (Priest) on Sep 23, 2004 at 17:02 UTC

    Your $a=<STDIN> leaves $a containing for instance "5\n", that is, including the newline.

    If you just chomp($a) your code would work. Unless there were trailing spaces or stuff ...

    Better to use $a+=0; to make it numeric.

    Oh, and as the monks are sure to be telling you: You should not use $a outside of sort, since that is a special variable there.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: Selecting a specific hash key to find a value
by Eimi Metamorphoumai (Deacon) on Sep 23, 2004 at 17:04 UTC
    zejames is right, but I'd also like to mention that if you're just using a hash to map consecutive integers to values, you might want to consider using an array instead. It can make it a lot easier to modify your values later (say, adding an item into the middle without renumbering everything manually).
    #!/usr/bin/perl print "Give me a number for the tile and I will tell you the color\n"; chomp($a=<STDIN>); print $a."\n"; @Number = qw(Red Yellow Green Black Purple Pink Brown Blue Orange); print $Number[$a-1]."\n";
A reply falls below the community's threshold of quality. You may see it by logging in.