Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Easy hash question (uninitialized value)

by WisDomSeeKer34 (Sexton)
on May 16, 2018 at 10:48 UTC ( #1214633=perlquestion: print w/replies, xml ) Need Help??
WisDomSeeKer34 has asked for the wisdom of the Perl Monks concerning the following question:

This should be very easy and yet I am stuck.
#!/usr/bin/env perl use strict; use warnings; use utf8; use feature 'say'; use File::Copy; my %j = ( 1 => 'vimplugins', 2 => "vimwiki", 3 => "vim", 4 => "bash", 5 => "latex", 6 => "perl", 7 => "regex", 8 => "git", 9 => "gnupg", 10 => "nmap", 11 => "tmux", 12 => "csssnippets", 13 => "htmlsnippets", 14 => "latexsnippets", 15 => "markdownsnippets", 16 => "perlsnippets", 17 => "vimsnippets", ); my $number = <STDIN>; say $number; my $x = $j{$number}; say $x; say "done";
I get the error message:" Use of uninitialized value $x in say". It doens't make sense to me.

Replies are listed 'Best First'.
Re: Easy hash question (uninitialized value)
by 1nickt (Monsignor) on May 16, 2018 at 11:53 UTC

    The solution, as others have said, is to use chomp to strip the newline characters from the input. But how could you have known that was the problem?

    When it doesn't make sense to you, add more debugging!

    my $number = <STDIN>; say ">$number<";

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Easy hash question (uninitialized value)
by marto (Archbishop) on May 16, 2018 at 10:55 UTC

    Use chomp:

    .... say 'Enter a number: '; my $number = <STDIN>; chomp( $number ); say $number; ....

    Produces:

    Enter a number: 4 4 bash done
Re: Easy hash question (uninitialized value)
by Lotus1 (Priest) on May 16, 2018 at 13:54 UTC

    Also check out the function exists. It lets you test if a key is in a hash. In this case it would help you debug but it's useful in general for validating hash keys, especially when they come from user input.

    I added the following at the end of your script:

    if( exists $j{$number} ) { say $j{$number}; say "done"; } else { say "<$number> is not a key in \%j" }

    Here is the result of running it:

    C:\usr\pm\hash>perl 1214633.pl 4 4 <4 > is not a key in %j
Re: Easy hash question (uninitialized value)
by Anonymous Monk on May 16, 2018 at 10:51 UTC
    chomp($number);

      Thank you all!

Re: Easy hash question (uninitialized value)
by marto (Archbishop) on May 16, 2018 at 10:55 UTC

    Use chomp:

    .... say 'Enter a number: '; my $number = <STDIN>; chomp( $number ); say $number; ....

    Produces:

    Enter a number: 4 4 bash done
Re: Easy hash question (uninitialized value)
by james28909 (Deacon) on May 16, 2018 at 19:02 UTC
    here is a great way to keep it neat and listed in numerical order as well
    #!/usr/bin/env perl use strict; use warnings; use utf8; use feature 'say'; use File::Copy; my %j = ( 1 => 'vimplugins', 2 => "vimwiki", 3 => "vim", 4 => "bash", 5 => "latex", 6 => "perl", 7 => "regex", 8 => "git", 9 => "gnupg", 10 => "nmap", 11 => "tmux", 12 => "csssnippets", 13 => "htmlsnippets", 14 => "latexsnippets", 15 => "markdownsnippets", 16 => "perlsnippets", 17 => "vimsnippets", ); ### PRINT THE SORTED HASH ### say "$_ - $j{$_}" for sort { $a <=> $b } keys(%j); ### GET USER SELECTION ### my $number = <STDIN>; chomp($number); ### PRINT OUT $j{$number} DIRECTLY INSTEAD OF COPYING IT TO ANOTHER BU +FFER ### say "\r\b\ryou picked -> $j{$number}\n" if exists $j{$number} or die " +selection <$number> does not exist in the list!\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1214633]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2018-07-22 01:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (451 votes). Check out past polls.

    Notices?