Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > string comparison

Reply
Thread Tools

string comparison

 
 
Shashank Khanvilkar
Guest
Posts: n/a
 
      09-29-2005
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";
}



 
Reply With Quote
 
 
 
 
Babacio
Guest
Posts: n/a
 
      09-29-2005
Shashank Khanvilkar <(E-Mail Removed)> 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";
> }


Try to write $a='cq~moa'; instead of $a='apple';

This should work, yes sir, because the value of 'apple'|'banana' is
'cq~moa'.

But if I guess what you really want to do, try that :

if ($a eq 'apple' || $a eq 'banana') { etc }

And read some documentation, this is in the second chapter of any
introduction to perl. For complete information about the operators,
perldoc perlop.

--
Bé erre hue ixe eu elle, Bruxelles.
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-29-2005
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

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-29-2005
Babacio wrote:
> Shashank Khanvilkar <(E-Mail Removed)> 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-30-2005
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Babacio wrote:
> > Shashank Khanvilkar <(E-Mail Removed)> 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.
 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      09-30-2005
Also sprach Anno Siegel:

> Gunnar Hjalmarsson <(E-Mail Removed)> 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=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      10-14-2005
Tassilo v. Parseval <(E-Mail Removed)> wrote:
> Also sprach Anno Siegel:
>
> > Gunnar Hjalmarsson <(E-Mail Removed)> 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/) {
>


If I was the code maintainer, I'd prefer to see:

if ( $a eq 'apple' or $a eq 'banana' ) {
...

It's concise, easy to read (it's practically English), and no modules required.

Of course, as the list of fruit grows longer, this becomes slighly
less appealing, but I missed the beginning of this thread, so I'm not
sure if that's relevant. If it was a long list of fruit I'd probably
put the details of the comparison into a subroutine:

if ( in_list( $a, @fruit ) ) {
...
}

The implementation of in_list() is left as an excercise for the reader.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-14-2005
Dave Weaver <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Tassilo v. Parseval <(E-Mail Removed)> wrote:
> > Also sprach Anno Siegel:
> >
> > > Gunnar Hjalmarsson <(E-Mail Removed)> 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/) {
> >

>
> If I was the code maintainer, I'd prefer to see:
>
> if ( $a eq 'apple' or $a eq 'banana' ) {
> ...
>
> It's concise, easy to read (it's practically English), and no modules required.
>
> Of course, as the list of fruit grows longer, this becomes slighly
> less appealing, but I missed the beginning of this thread, so I'm not
> sure if that's relevant. If it was a long list of fruit I'd probably
> put the details of the comparison into a subroutine:


For a long list the standard solution is a hash:

my %is_fruit = (
apple => 1,
banana => 1,
# ...
);
if ( $is_fruit{ $a} ) {
# ...
}

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.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-14-2005
Dave Weaver schreef:

> If I was the code maintainer, I'd prefer to see:
>
> if ( $a eq 'apple' or $a eq 'banana' ) {



This looks too great:

if ( $a in ('apple', 'banana') ) {

so how about the maintainability of:

my @fruit = qw(apple banana);

my $regex = '^('. join('|', @fruit) .')$';

if ( $a =~ /$regex/ ) {

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-14-2005
Dr.Ruud wrote:
> Dave Weaver schreef:
>
>> If I was the code maintainer, I'd prefer to see:
>>
>> if ( $a eq 'apple' or $a eq 'banana' ) {

[...]
> so how about the maintainability of:
>
> my @fruit = qw(apple banana);
>
> my $regex = '^('. join('|', @fruit) .')$';
>
> if ( $a =~ /$regex/ ) {


Two problems:
- REs are the wrong tool to do string comparion
- and they will yield wrong results as soon as the search text contains
RE-special characters

jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
False Positives From String Comparison using string.Equals() Smithers ASP .Net 12 07-07-2009 01:23 AM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Is there systematic performance comparison of std::string and c style string? yu_kuo@sina.com C++ 21 08-18-2007 04:32 PM
simple jsp string comparison Jason Java 2 09-20-2003 12:16 AM



Advertisments