in reply to Conditional within Hash definition
If you run to more than two conditions, you can use a mapping hash
use strict; use warnings; # the simple case, as requested my $rain = int rand 2; my %rain_map = ( 0 => 'dry', 1 => 'wet', ); my %Weather = ( precipitation => $rain_map{$rain} ); print "Simple weather($rain) is $Weather{precipitation}\n"; # a little more my $precip = int rand 5; my %precip_map = ( 0 => 'dry', 1 => 'wet fog', 2 => 'wet rain', 3 => 'wet hail', 4 => 'plague of locusts', ); my %More_Weather = ( precipitation => $precip_map{$precip} ); print "More weather($precip) is $More_Weather{precipitation}\n";
Cheers,
R.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Conditional within Hash definition
by tangent (Parson) on Jan 28, 2014 at 17:06 UTC | |
by Random_Walk (Prior) on Jan 29, 2014 at 09:56 UTC | |
by Anonymous Monk on Jan 29, 2014 at 13:45 UTC | |
|
Re^2: Conditional within Hash definition
by veryan (Initiate) on Jan 28, 2014 at 16:58 UTC |