Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

perl syntax help for ternary operator

by ghosh123 (Monk)
on Oct 09, 2014 at 07:44 UTC ( #1103265=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monk
A table cell in my web application is showing the value based on a conditional statement. Please refer to the code below :

<td align=center>&nbsp; @{[ ($string eq 'level3' )? 'level3 stage' : +'Edit']}</td>

Now assume that $string may contain two more values, say for example 'level1' and 'level2' .
And each such cases the value shown in the table cell would be 'level1 stage' and 'level2 stage' respectively otherwise 'Edit'.
How would I change the above syntax to accommodate two more conditions to check the other possible values in $string and show the desired results.

thanks.

Replies are listed 'Best First'.
Re: perl syntax help for ternary operator
by Corion (Patriarch) on Oct 09, 2014 at 07:47 UTC

    You change the code that creates the HTML string to set the value outside of the interpolation:

    my %descriptions= ( 'level3' => 'level3 stage', 'level2' => 'level2 stage', 'level1' => 'level1 stage', ); my $visual= $decsriptions{ $string } || 'Edit'; ... print "<td align=center>&nbsp;$visual</td>

    Maybe it would be a good time now to look at templating systems.

Re: perl syntax help for ternary operator
by choroba (Archbishop) on Oct 09, 2014 at 07:51 UTC
    You can use a regex match in the ternary operator, too:
    $string =~ /^level[123]$/ ? "$string stage" : 'Edit'
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: perl syntax help for ternary operator
by AnomalousMonk (Bishop) on Oct 09, 2014 at 12:59 UTC

    Here's how ternary operators can be chained. (And yes, wrapping this up in a subroutine is a very good idea, but the operator chain itself could be in-lined.)

    c:\@Work\Perl>perl -wMstrict -le "my $s = qq{a ${ \str('tilt2') } operation needs @{[ str('x') ]} help} +; print qq{'$s'}; ;; sub str { my ($s) = @_; ;; return $s eq 'level3' ? 'level3 stage' : $s eq 'tilt2' ? 'tilt2 snafu' : ! defined $s ? 'undefined' : qq{Edit '$s'} ; } " 'a tilt2 snafu operation needs Edit 'x' help'
    See Conditional Operator in perlop.

Re: perl syntax help for ternary operator
by Anonymous Monk on Oct 09, 2014 at 08:45 UTC

    How would I change the above syntax to accommodate two more conditions to check the other possible values in $string and show the desired results.

    make a subroutine  <td> @{GhoshLevels()}  </td>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1103265]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2022-05-25 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?