in reply to Conditional within Hash definition

Using the ternary operator:
my $rain = 1; my %Weather = ( 'precipitation' => ($rain == 0) ? 'dry' : 'wet', );
Or even:
my $rain = 1; my %Weather = ( 'precipitation' => $rain ? 'wet' : 'dry', );
The way to use the if/elsif construct:
my $precipitation; if ($rain == 0) { $precipitation = 'dry'; } elsif ($rain == 1) { $precipitation = 'wet'; }
See perldoc for Conditional Operator and Compound Statements, and also strict and warnings

Replies are listed 'Best First'.
Re^2: Conditional within Hash definition
by veryan (Initiate) on Jan 28, 2014 at 16:56 UTC
    So many thanks - much appreciated!