Solved

Why doesn´t my IF statement work

Posted on 1998-07-25
4
195 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

724 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