in reply to Getting values from a Hash from user input
Another way is to use Perl's defined-or operator (//):
use strict; use warnings; my %words = ( 'hello' => 'world' ); print 'type a word: '; chomp( my $inData = <> ); print $words{$inData} // "Key '$inData' doesn't exist.\n";
If the key's defined, it's associated value will be printed, otherwise the 'default' string will be printed.
Hope this helps!
Edit: //= -> //. Thank you, Eily.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Getting values from a Hash from user input
by Eily (Monsignor) on Nov 21, 2013 at 21:33 UTC | |
by Kenosis (Priest) on Nov 21, 2013 at 21:36 UTC | |
by blueblue (Initiate) on Nov 21, 2013 at 22:01 UTC |