Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

printing hash values and keys from user

by pinnacle (Acolyte)
on Nov 01, 2010 at 18:13 UTC ( [id://868836]=perlquestion: print w/replies, xml ) Need Help??

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

Script
1 #!/usr/bin/perl -w 2 3 use strict; 4 5 my %course = ( 6 2 => 'c', 7 1 => 'unix', 8 3 => 'shell', 9 4 => 'perl', 10 ); 11 12 while ( (my $key, my $value) = each(%course) ) { 13 print "$key => $value\n"; 14 } 15 16 print "\nSorted values of hash:\n"; 17 foreach my $val (sort values %course){ 18 print "$val\n"; 19 } 20 print "\nEnter Key: "; 21 my $type = <stdin>; 22 print "you will be taking $course{$type} this semester\n";
Script Result
:~/pbe/chap5$ ./exer5.pl 4 => perl 1 => unix 3 => shell 2 => c Sorted values of hash: c perl shell unix Enter Key: 2 Use of uninitialized value within %course in concatenation (.) or stri +ng at ./exer5.pl line 22, <stdin> line 1. you will be taking this semester
Question

Can anyone explain why I am always getting above error of "uninitialized value", I know I am doing some small mistake, any input is highly appreciated

Replies are listed 'Best First'.
Re: printing hash values and keys from user (OT: stdin vs. STDIN)
by toolic (Bishop) on Nov 01, 2010 at 18:23 UTC
    You get this error because $type has a trailing newline character, which means that $type does not match any of your hash keys. You should chomp your input:
    my $type = <stdin>; chomp $type;

    See Basic debugging checklist Tip #3.

    Update: I was surprised to see that lowercase stdin works just like uppercase STDIN. All the docs seem to just use uppercase for this special filehandle. Can some monk show me where this is documented?

      See I/O Operators in perlop, about the ninth paragraph:

      The filehandles STDIN, STDOUT, and STDERR are predefined. (The filehandles "stdin", "stdout", and "stderr" will also work except in packages ...)
        ++AnomalousMonk. It is probably a good practice to just stick with the uppercase version, to avoid different behavior in and out of packages.

      Thanks All

Re: printing hash values and keys from user
by liverpole (Monsignor) on Nov 01, 2010 at 18:22 UTC
    Hi pinnacle,

    Yes, it's because you are using a key that has a newline on it.

    Try doing:

    my $type = <stdin>; chomp $type;

    before using the key.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: printing hash values and keys from user
by Anonymous Monk on Nov 01, 2010 at 18:21 UTC
    The value read from stdin ends with a newline, so there's no entry for it in the hash... --> chomp

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://868836]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-03-19 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found