Solved

Why doesn´t my IF statement work

Posted on 1998-07-25
4
175 Views
Last Modified: 2010-03-05
Hi
I´m trying to make a kind of select expression with an if statement.
I have a While loop and I just want to print out those lines which matches the if statement.


if (("apple" || "banana") eq "banana") {
print "yes it worked";
} else {
print "No, it didn´t work";
}
This will not print out "yes it worked". I can solve it with the
elsif-thing, but because the if statement is inside a while-loop its not possible (as far as I know) to make it work.

I hope this information is sufficient.

Regards
Claes Lindvall
Sweden
0
Comment
Question by:cl071997
  • 2
4 Comments
 

Author Comment

by:cl071997
ID: 1208784
Edited text of question
0
 
LVL 6

Accepted Solution

by:
alamo earned 100 total points
ID: 1208785
This will work:

f ("apple" eq "banana" || "banana" eq "banana") {
    print "yes it worked";
} else {
    print "No, it didn´t work";
}

And I don't know why you say elsif won't work - the following works as well:
if ("apple" eq "banana") {
    print "yes it worked";
} elsif ("banana" eq "banana") {
    print "yes it worked";
} else {
    print "No, it didn't work";
}

The reason your original didn't work is ("apple" || "banana") was _always_ "apple", and therefore it was never equal to "banana".

Hope this helps!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208786
#another way to do it:
@fruit{qw(apple banana)} = (1) x 2;
if( $fruit{banana} ){
   print "yes it worked"
}else{
   print "No, it didn´t work"
}
0
 

Author Comment

by:cl071997
ID: 1208787
Great. Thank you so much...

Claes Lindvall
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question