Solved

Why doesn´t my IF statement work

Posted on 1998-07-25
4
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

696 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