From: Shashank Khanvilkar on
Hi,
I think i am doing something wrong here. Can anyone please let me know what

$a = "apple";

if ($a eq ('apple')){
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";
}



From: Paul Lalli on
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

From: Gunnar Hjalmarsson on
Babacio wrote:
> Shashank Khanvilkar <shashank(a)mia.ece.uic.edu> writes:
>>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";
>>}
>
> But if I guess what you really want to do, try that :
>
> if ($a eq 'apple' || $a eq 'banana') { etc }

or this:

if ( grep $a eq $_, ('apple', 'banana') ) {

(perldoc -f grep)

> And read some documentation,

Indeed.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
From: Anno Siegel on
Gunnar Hjalmarsson <noreply(a)gunnar.cc> wrote in comp.lang.perl.misc:
> Babacio wrote:
> > Shashank Khanvilkar <shashank(a)mia.ece.uic.edu> writes:
> >>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";
> >>}
> >
> > But if I guess what you really want to do, try that :
> >
> > if ($a eq 'apple' || $a eq 'banana') { etc }
>
> or this:
>
> if ( grep $a eq $_, ('apple', 'banana') ) {
>
> (perldoc -f grep)

....or use Quantum::Superposition

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
From: Tassilo v. Parseval on
Also sprach Anno Siegel:

> Gunnar Hjalmarsson <noreply(a)gunnar.cc> wrote in comp.lang.perl.misc:
>> Babacio wrote:

>> > But if I guess what you really want to do, try that :
>> >
>> > if ($a eq 'apple' || $a eq 'banana') { etc }
>>
>> or this:
>>
>> if ( grep $a eq $_, ('apple', 'banana') ) {
>>
>> (perldoc -f grep)
>
> ...or use Quantum::Superposition

Yuck. I'd suggest

use List::MoreUtils qw/any/;

if (any { $a eq $_ } qw/apple banana/) {

Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);