Solved

Why doesn´t my IF statement work

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now