[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

Why doesn´t my IF statement work

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
cl071997
Asked:
cl071997
  • 2
1 Solution
 
cl071997Author Commented:
Edited text of question
0
 
alamoCommented:
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
 
ozoCommented:
#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
 
cl071997Author Commented:
Great. Thank you so much...

Claes Lindvall
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now