Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: true/false condition - what am I doing wrong?

by Marshall (Canon)
on Dec 08, 2011 at 15:30 UTC ( [id://942465]=note: print w/replies, xml ) Need Help??


in reply to true/false condition - what am I doing wrong?

This is an abuse of the ternary operator.

Normally in this situation, you want to choose between 3 choices, not two!

Use the ternary operator to set a variable dependent upon a true/ false condition.
A common use would be like this:
($today eq 'Monday') ? $firstDay =1 : $firstDay =0;

That would be preferred over:
$firstDay=0;
$firstDay=1 if ($today eq 'Monday');

Use if and if/elsif/else conditions for more complicated things.

#!/usr/bin/perl -w use strict; decide ('add'); decide ('edit'); decide ('bad input nothing'); print "\n"; decide2 ('add'); decide2 ('edit'); decide2 ('bad input nothing'); sub decide { my $input = shift; print "invalid input\n" if ($input ne "add" and $input ne "edit"); print "doing an add\n" if $input eq "add"; print "doing an edit\n" if $input eq "edit"; } sub decide2 { my $input = shift; if ($input eq 'add') { print "the add code goes here\n"; } elsif ($input eq 'edit') { print "the edit code goes here\n"; } else { print "the invalid input code goes here\n"; } } __END__ doing an add doing an edit invalid input the add code goes here the edit code goes here the invalid input code goes here

Replies are listed 'Best First'.
Re^2: true/false condition - what am I doing wrong?
by choroba (Cardinal) on Dec 08, 2011 at 16:01 UTC
    A common use would be like this:
    ($today eq 'Monday') ? $firstDay =1 : $firstDay =0;
    I have always thought the common use is
    $firstDay = $today eq 'Monday' ? 1 : 0;
      correct - tired this morning.. been pulling late nights on a db project...
      main point is that this is not the right place for 'edit' vs 'add'.

      Update: even more uses are possible:

      #!/usr/bin/perl -w use strict; sub is_name_valid { return $_[0]; #just a dummy } print "the name ",is_name_valid(0) ? "was" :"was not"," valid\n"; # the name was not valid print "the name ",is_name_valid(1) ? "was" :"was not"," valid\n"; # the name was valid
Re^2: true/false condition - what am I doing wrong?
by Anonymous Monk on Dec 08, 2011 at 15:49 UTC
Re^2: true/false condition - what am I doing wrong?
by Perlbotics (Archbishop) on Dec 08, 2011 at 23:19 UTC

    The ternary operator is stackable. So for three choices, the following might be bearable:

    use strict; use warnings; for my $type ( qw(add edit nada) ) { my $redo = $type eq 'add' ? 'wiki_add2' : $type eq 'edit' ? 'wiki_edit2' : 'error'; print "$type -> $redo\n"; } __END__ add -> wiki_add2 edit -> wiki_edit2 nada -> error

    But as AM points out, the OP's task seems to call for a dispatch table.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found