Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Getting values from a Hash from user input

by Eily (Monsignor)
on Nov 21, 2013 at 21:33 UTC ( [id://1063814]=note: print w/replies, xml ) Need Help??


in reply to Re: Getting values from a Hash from user input
in thread Getting values from a Hash from user input

The defined-or operator is just //. $lvalue //= "value"; actually means $lvalue = "value" unless defined $lvalue;. This means that you actually write "Key '$inData' doesn't exist.\n" in the hash :

use Data::Dumper; my %words = ( 'hello' => 'world' ); print 'type a word: '; chomp( my $inData = <> ); print $words{$inData} //= "Key '$inData' doesn't exist.\n"; print Dumper \%words;
type a word: Bonjour Key 'Bonjour' doesn't exist. $VAR1 = { 'hello' => 'world', 'Bonjour' => 'Key \'Bonjour\' doesn\'t exist. ' };
You meant print $words{$inData} // "Key '$inData' doesn't exist.\n"; which works fine as long as undef isn't a valid value.

Replies are listed 'Best First'.
Re^3: Getting values from a Hash from user input
by Kenosis (Priest) on Nov 21, 2013 at 21:36 UTC

    Thanks for this catch! Corrected...

      Thanks for help guys

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found