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


In reply to printing hash values and keys from user by pinnacle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.