develooper Front page | perl.beginners | Postings from April 2012

Re: Using the ternary operator to concat a string not working like Ithink?

Thread Previous
From:
Uri Guttman
Date:
April 7, 2012 10:26
Subject:
Re: Using the ternary operator to concat a string not working like Ithink?
Message ID:
4F8078A8.5080004@stemsystems.com
On 04/07/2012 06:54 AM, Peter Scott wrote:
> On Wed, 04 Apr 2012 03:04:42 -0400, Uri Guttman wrote:

>> that code is not a good use of ?: at all so use if/else.
>
> Right.  And if you want the single statement succinctness, use and/or:
>
> % perl -le '%test = qw(one first two second); $test{one} eq "first" and
> $test{one} .= " is the worst" or $test{two} .= " is the best"; print for
> values %test'
> first is the worst
> second
>
> % perl -le '%test = qw(one third two second); $test{one} eq "first" and
> $test{one} .= " is the worst" or $test{two} .= " is the best"; print for
> values %test'
> third
> second is the best

however much i respect peter, i disagree here. and/or are low precedence 
and work well here but using them for completely different assignments 
just to make a single statement is overkill. maybe in a one liner but 
how many one liners need code this complex (if needed, it shouldn't be a 
one liner). the code here should be a classic if/else and no other way.

uri

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About