[tpm] Is there a three-way version of...

Dave Doyle dave.s.doyle at gmail.com
Fri May 29 11:24:10 PDT 2009


Addendum:

You can chain the ternary operator as much as you want.  Laying out in
a table-like way is adviced.

my $title = $condition1 ? 'result1'
              : $condition2 ? 'result2'
              : $condition 3 ? 'result3'
             # ... more lines here
              :                      'default';

--
dave.s.doyle at gmail.com



On Fri, May 29, 2009 at 2:20 PM, Dave Doyle <dave.s.doyle at gmail.com> wrote:
> In one line:
>
> my $title= $a ? 'bing' : $b ? 'bang' : 'bong';
>
> but more readable as per PBP:
>
> my $title = $a ? 'bing'
>             :  $b ? 'bang'
>             :         'bong';
>
>
> D
>
> dave.s.doyle at gmail.com
>
>
> On Fri, May 29, 2009 at 2:15 PM, Madison Kelly <linux at alteeve.com> wrote:
>>
>> Madison Kelly wrote:
>>>
>>> my $foo=$bar ? "bing" : "bang";
>>>
>>> I am probably trying to extend what the above syntax was designed to address, but I am curious. I've got three strings I want to show based on the viewing user's credentials. I know I can use:
>>>
>>> my $title="";
>>> if ($a)
>>> {
>>>    $title="bing";
>>> }
>>> elsif ($b)
>>> {
>>>    $title="bang";
>>> }
>>> else
>>> {
>>>    $title="bong";
>>> }
>>>
>>> But it's quite a bit more code.
>>>
>>> Thanks all!
>>>
>>> Madi
>>
>> Sorry to reply to my own question, but I did think of:
>>
>> my $title="bong";
>> $title="bang" if $b;
>> $title="bing" if $a;
>>
>> Though I would still be interested in another way more similar to:
>>
>> my $foo=$bar ? "bing" : "bang";
>>
>> Madi
>> _______________________________________________
>> toronto-pm mailing list
>> toronto-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/toronto-pm
>


More information about the toronto-pm mailing list