http://www.perlmonks.org?node_id=1067720

geistberg has asked for the wisdom of the Perl Monks concerning the following question:

I am getting some unexpected results

code

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my ( $broken, $works ); my @list = qw( this that another ); for ( @list ) { $broken->{'this'} = $_ eq 'this' ? 1 : 0; $broken->{'that'} = $_ eq 'that' ? 1 : 0; $broken->{'another'} = $_ eq 'another' ? 1 : 0; } for ( @list ) { $works->{'this'} = 1 if $_ eq 'this'; $works->{'that'} = 1 if $_ eq 'that'; $works->{'another'} = 1 if $_ eq 'another'; } print Dumper $broken, $works;

result

perl example.pl $VAR1 = { 'another' => 1, 'that' => 0, 'this' => 0 }; $VAR2 = { 'another' => 1, 'that' => 1, 'this' => 1 };

I expected all the keys to be 1 in this case on the broken hash ref, but two of the 0's got through. I like the ternary operator but I think I made it angry, any thoughts?