in reply to true/false condition - what am I doing wrong?
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 | |
by Marshall (Canon) on Dec 08, 2011 at 16:58 UTC | |
|
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 |