Re: string comparison
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 29 Sep 2005 10:17:04 -0700
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
.
- References:
- string comparison
- From: Shashank Khanvilkar
- string comparison
- Prev by Date: Re: regex replace credit card numbers with *
- Next by Date: Re: Order of Elements in Hash
- Previous by thread: string comparison
- Next by thread: Re: string comparison
- Index(es):
Relevant Pages
|
|