in reply to Initializing a hash using conditions
I was wondering if I can...
the answer is... why not just try it and see if you can. If it is wrong, Perl will complain, especially if you have used -w and strict.
The above won't compile. However, if you change it ever so slightly, it will. Keep in mind that the right side of => or any element in an array, for that matter, can be the result of an expression. Convert your if... else... statement into the ternary form and it will work...
%hash = ( 'abc' => !$i ? 7 : '', 'hk' => 5, 'jk' => 6 ); # a ternary expression is $var = condition ? if_true : if_false
Good luck.
|
|---|