error in <stdin>: use of uninitialised value within %lop in concatenation or string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saurabhm2623
    New Member
    • Mar 2013
    • 2

    error in <stdin>: use of uninitialised value within %lop in concatenation or string

    the following prog. i was trying but after entering the value alpha or anything generates an error that goes like : use of uninitialised value within %lop in conactenation or string at ...".please help.is there any problem with using the value obtained in scalar via <STDIN>??


    Code:
    use warnings;
    use strict;
    my $k;
    my %lop=(
    alpha=>2143,
    beta=>456843,
    gamma=>78458,
    delta=>6453,
    theta=>4658);
    
    
    $k=<STDIN>;
    
    
    
    print"\n $lop{$k}";
    Last edited by Rabbit; Mar 22 '13, 04:34 PM. Reason: Please use code tags when posting code.
  • sathishkumar se
    New Member
    • Dec 2011
    • 10

    #2
    Hi,

    Can you try this below code.
    Code:
    use warnings;
    use strict;
    my $k;
    my %lop=(
    alpha=>2143,
    beta=>456843,
    gamma=>78458,
    delta=>6453,
    theta=>4658);
    
    
    $k=<STDIN>;
    chomp($k);
    
    
    print"\n $lop{$k}";

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      I don't know perl but on line 4 you use %lop but on line 6 you use $lop. Wouldn't they have to be the same? Also you're using different brackets. Do those have to be the same?

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        The explanation of hashes can get lengthy and confusing, but I'll try to keep it short and simple.
        • Hashes are unordered lists of key/value pairs.
        • % is the sigil used to denote a hash (as a whole).
        • The parens used in the hash assignment indicate list context.
        • Within the parens you have a comma separated list of key pairs which are separated by => (known as the fat comma).
        • When referring to individual pairs, you use the $ sigil instead of the % sigil and you use the { } braces around the key instead of the parens.

        Comment

        • saurabhm2623
          New Member
          • Mar 2013
          • 2

          #5
          thanks a lot it solved the problem. but i would like to know what was the mistake in mine. also what did chomp($k) do. (i'm just starting in the basics of perl so dont know much would appreciate your explanation.).

          Comment

          • sathishkumar se
            New Member
            • Dec 2011
            • 10

            #6
            Hi,

            chomp($k);
            The above code remove the newline character in the $k variable.

            Comment

            Working...