CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
The use of '?' and ':'

 



joeb
New User

Mar 3, 2008, 6:39 PM

Post #1 of 4 (255 views)
The use of '?' and ':' Can't Post

Can someone tell me what the '?' and ':' mean in this statement?

$cf ? ($cfg = $cf) : ($cfg = 'signup');

I've tried to google it, but haven't found any reference for the setup of this line of code. Any help appreciated.

Joe


hydpm
User

Mar 3, 2008, 6:47 PM

Post #2 of 4 (254 views)
Re: [joeb] The use of '?' and ':' [In reply to] Can't Post

?: is ternerary operator in perl which takes three operands

For ex: Script that checks biggest of the two numbers and stores in other variable.

$a = 10;$b = 20;

$Big = ($a>$b)?$a:$b;

print $big;

--------------------------


joeb
New User

Mar 3, 2008, 7:44 PM

Post #3 of 4 (252 views)
Re: [joeb] The use of '?' and ':' [In reply to] Can't Post

Thank you wingsof5r!

I've been looking for the answer to that for months, and you answered in minutes :)

Thanks again!


eWish
User

Mar 3, 2008, 8:37 PM

Post #4 of 4 (249 views)
Re: [joeb] The use of '?' and ':' [In reply to] Can't Post

Essentially it is the same as if-else. These two snippets of code are equal.


Code
my $value1 = $value1 == 0 ? 'false' : 'true';



Code
if ($value1 == 0) { 
$value1 = 'False';
} esle {
$value1 = 'True';
}


perldoc perlop


--Kevin

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives