Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Easy hash question (uninitialized value)

by WisDomSeeKer34 (Sexton)
on May 16, 2018 at 10:48 UTC ( [id://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 (Canon) 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 (Cardinal) 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 (Vicar) 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 (Cardinal) 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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1214633]
Approved by Corion
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: (5)
As of 2024-09-15 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.