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:
about (if)

 



makals
Novice

Jan 1, 2011, 11:29 PM

Post #1 of 9 (1017 views)
about (if) Can't Post

hello

i have a question ?

if ($numeric) {

@file = sort { $a <=> $b } @file;

} else {

@file = sort @file;



}




what is it mean by this (if) ,we didnt put any oprator or something in the ()?

please everybody give me direct answer.



Zhris
Enthusiast

Jan 2, 2011, 6:16 AM

Post #2 of 9 (1005 views)
Re: [makals] about (if) [In reply to] Can't Post

Hi,

In an "if" statement we test whether or not the condition within the parentheses is "true" before executing its child block of code. Therefore the condition "if ($numeric) {" checks if the variable $numeric has a "true" value. There are a number of values that can be equated as being "true":


Code
my $numeric; 

#some true values
$numeric = 1;
$numeric = "77";
$numeric = 'Hello world';
$numeric = true;

if ($numeric) { #this condition WILL be executed because $numeric has a true value }

#some false values
$numeric = 0;
$numeric = '';
$numeric = undef;
$numeric = false;

if ($numeric) { #this condition WILL NOT be executed because $numeric has a false value }


Its the same for common operator based "if" statements:


Code
my $numeric = 4; 

if ($numeric > 2) { #this condition WILL be executed because its true that $numeric is more than 2 }

if ($numeric < 2) { #this condition WILL NOT be executed because its false that $numeric is less than 2 }


Hope this helps,

Chris


(This post was edited by Zhris on Jan 2, 2011, 6:17 AM)


BillKSmith
Veteran

Jan 2, 2011, 1:22 PM

Post #3 of 9 (991 views)
Re: [Zhris] about (if) [In reply to] Can't Post

I disagree on one point. Strange as it seems, the condition should be TRUE when $numeric = false.

Without strictures, the bare word is treated as a string. The string 'false' is a true value because it is not one of the five false values.

Refer: perldoc perlsyn

Quote
Truth and Falsehood

The number 0, the strings '0' and '', the empty list (), and undef are all false in a boolean context. All other values are true. Negation of a true value by ! or not returns a special false value. When evaluated as a string it is treated as '', but as a number, it is treated as 0.

Good Luck,
Bill


makals
Novice

Jan 2, 2011, 11:05 PM

Post #4 of 9 (986 views)
Re: [Zhris] about (if) [In reply to] Can't Post

thanks everybody.


Zhris
Enthusiast

Jan 3, 2011, 4:21 PM

Post #5 of 9 (975 views)
Re: [BillKSmith] about (if) [In reply to] Can't Post

Thats interesting. I've always used 1 or 0 as a boolean value and assumed that true or false would be acceptable.

Chris


rovf
Veteran

Jan 4, 2011, 7:40 AM

Post #6 of 9 (961 views)
Re: [Zhris] about (if) [In reply to] Can't Post

1 is an example for true, and 0 is an example for false, so 1 and 0 work.

Other forms are possible too. For instance "0 but true" is true, while "0 but honestly true" is false (if you have in inclination to bizzarre coding).


BillKSmith
Veteran

Jan 4, 2011, 8:58 AM

Post #7 of 9 (959 views)
Re: [rovf] about (if) [In reply to] Can't Post

Your second example appears to contradict the documentation that I quoted above. That tempted me to try the "bizarre coding." I cannot make it work as you describe. Please post a sample of this code to help us see how this behavior can work and still be consistent with the documentation.
Good Luck,
Bill


rovf
Veteran

Jan 4, 2011, 10:14 AM

Post #8 of 9 (958 views)
Re: [BillKSmith] about (if) [In reply to] Can't Post

You caught me. I didn't think carefully enough. I was mislead by the convention that, if you need a zero which evaluates to true in a logical context, you use the string "0 but true":


Code
my $c="0 but true"; 
if($c) {
say 'true';
} else {
say $c+5
}


prints 5. However, this is just a convention. Any string starting with 0, followed by a space, would serve the same purpose.

So, less bizzarre than I hoped. Sigh.


Zhris
Enthusiast

Jan 4, 2011, 2:55 PM

Post #9 of 9 (952 views)
Re: [rovf] about (if) [In reply to] Can't Post

I often use "0 but true" particularly whilst returning the result of a mathematical sum from a subroutine (0 is a valid result), and where I can't be certain that the return value is defined, therefore I check whether the value is true/defined after it has been returned. If that makes sense.

Chris


(This post was edited by Zhris on Jan 4, 2011, 2:57 PM)

 
 


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

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