Re: string comparison



Shashank Khanvilkar wrote:
> Hi,
> I think i am doing something wrong here. Can anyone please let me know what
>
> $a = "apple";
>
> if ($a eq ('apple')){

Those inner parentheses are unneeded, and serve only to clutter the
expression.

> print "right choice\n";
> }else{
> print "wrong choice\n";
> }
>
> This above prints "right choice". But the below prints "wrong choice".
> How can i make the below program print "right choice".
>
> $a = "apple";
>
> if ($a eq ('apple'|'banana')){
> print "right choice\n";
> }else{
> print "wrong choice\n";
> }

The eq operator tests for equality. The | operator does a bit-wise
'or' on its arguments. Therefore, the string you are testing for
equality to eq is: 'cq~moa' ('a' | 'b' = 'c', 'p' | 'a' = 'q',
etcetera).

I *think* what you're looking for is a regular expression, or pattern
match. You want to determine if $a contains either of your two
strings:

if ($a =~ /apple|banana/) {
print "\$a contains either 'apple' or 'banana'\n";
}

Read more on regular expressions in a variety of documentation:
perldoc perlre
perldoc perlretut
perldoc perlreref
perldoc perlrequick


Paul Lalli

.



Relevant Pages

  • Re: doubt in subtitution operator
    ... :> will tell you all the gory details. ... :The regex part of the code above is "h". ... :(that has a regular expression as one of its operands). ... And yet the ismx modifiers are described in perldoc perlre. ...
    (comp.lang.perl.misc)
  • Re: regular expression to capture invalid emails
    ... I want to create a regular expression that ... > matches any email address that ends in two characters like antyhing@home.tv ... See "perldoc perlre" for details, and feel free to read it before ... posting. ...
    (comp.lang.perl.misc)
  • Re: regular expressions
    ... Sokar wrote: ... > I have the string ... > I was wondering if anyone knew a regular expression that would remove the ... Have you read the FAQ and tried command "perldoc perlre"? ...
    (comp.lang.perl.misc)
  • RE: extract zip file
    ... Treat string as multiple lines ... perldoc perlre. ... Joe ... > Subject: Re: extract zip file ...
    (perl.beginners)
  • Re: extract zip file
    ... Treat string as multiple lines ... So, use single quotes. ... perldoc perlre. ... Joe ...
    (perl.beginners)