Melly has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monkees,
Why does the following code print 10 bars for the first section, yet behaves as I expected for the second?
#Huh? 10 bars, WTF? for(1..10){ $_>5 ? $x='foo' : $x='bar'; print "$_:$x\n"; } # That's better - 5 foos and 5 bars for(1..10){ $x=($_>5 ? 'foo' : 'bar'); print "$_:$x\n"; }
Tom Melly, tom@tomandlu.co.uk
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Conditional Operator Confusion
by davorg (Chancellor) on Nov 29, 2006 at 12:14 UTC | |
by Melly (Chaplain) on Nov 29, 2006 at 12:39 UTC | |
by Anonymous Monk on Nov 29, 2006 at 12:56 UTC | |
Re: Conditional Operator Confusion
by GrandFather (Saint) on Nov 29, 2006 at 12:47 UTC | |
Re: Conditional Operator Confusion
by MonkE (Hermit) on Nov 29, 2006 at 13:59 UTC | |
Re: Conditional Operator Confusion
by Herkum (Parson) on Nov 29, 2006 at 13:17 UTC | |
by Melly (Chaplain) on Nov 29, 2006 at 15:48 UTC | |
by Herkum (Parson) on Nov 30, 2006 at 16:44 UTC | |
by Melly (Chaplain) on Nov 30, 2006 at 17:03 UTC | |
by ikegami (Patriarch) on Nov 30, 2006 at 17:12 UTC | |
|
Back to
Seekers of Perl Wisdom