string comparison

Discussion in 'Perl Misc' started by Shashank Khanvilkar, Sep 29, 2005.

  1. 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";
    }
     
    Shashank Khanvilkar, Sep 29, 2005
    #1
    1. Advertising

  2. Shashank Khanvilkar

    Babacio Guest

    Shashank Khanvilkar <> 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.
     
    Babacio, Sep 29, 2005
    #2
    1. Advertising

  3. Shashank Khanvilkar

    Paul Lalli Guest

    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
     
    Paul Lalli, Sep 29, 2005
    #3
  4. Babacio wrote:
    > Shashank Khanvilkar <> 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
     
    Gunnar Hjalmarsson, Sep 29, 2005
    #4
  5. Shashank Khanvilkar

    Anno Siegel Guest

    Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
    > Babacio wrote:
    > > Shashank Khanvilkar <> 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.
     
    Anno Siegel, Sep 30, 2005
    #5
  6. Also sprach Anno Siegel:

    > Gunnar Hjalmarsson <> 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);
     
    Tassilo v. Parseval, Sep 30, 2005
    #6
  7. Shashank Khanvilkar

    Dave Weaver Guest

    Tassilo v. Parseval <> wrote:
    > Also sprach Anno Siegel:
    >
    > > Gunnar Hjalmarsson <> 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.
     
    Dave Weaver, Oct 14, 2005
    #7
  8. Shashank Khanvilkar

    Anno Siegel Guest

    Dave Weaver <> wrote in comp.lang.perl.misc:
    > Tassilo v. Parseval <> wrote:
    > > Also sprach Anno Siegel:
    > >
    > > > Gunnar Hjalmarsson <> 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.
     
    Anno Siegel, Oct 14, 2005
    #8
  9. Shashank Khanvilkar

    Dr.Ruud Guest

    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."
     
    Dr.Ruud, Oct 14, 2005
    #9
  10. 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
     
    Jürgen Exner, Oct 14, 2005
    #10
  11. Shashank Khanvilkar

    Dr.Ruud Guest

    Jürgen Exner:
    > Dr.Ruud:


    >> my @fruit = qw(apple banana);
    >>
    >> my $regex = '^('. join('|', @fruit) .')$';
    >>
    >> if ( $a =~ /$regex/ ) {

    >
    > Two problems:
    > - REs are the wrong tool to do string comparion


    What are the big wrongs?


    > - and they will yield wrong results as soon as the search text
    > contains RE-special characters


    In procmail I can use $\var to (almost) unharm this. Of course there
    is no need to show a Perl way to do something similar, if the first
    problem is that big.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Oct 14, 2005
    #11
  12. Shashank Khanvilkar

    Anno Siegel Guest

    Dr.Ruud <> wrote in comp.lang.perl.misc:
    > 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/ ) {


    It's fine, though someone might come up with "banana[puertorico]" vs.
    "banana[brazil]" and mess up the regex. I'd also prefer non-capturing
    parentheses:

    my $regex = '^(?:' . join( '|', map quotemeta, @fruit) . ')$';

    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.
     
    Anno Siegel, Oct 14, 2005
    #12
  13. Dr.Ruud wrote:
    > Jürgen Exner:
    >> Two problems:
    >> - REs are the wrong tool to do string comparion

    >
    > What are the big wrongs?


    You don't use a gun to swat flies. You use a fly swater.

    Perl has a perfectly fine eq operator for comparing strings. There is no
    need to launch the big and expensive RE gun and then jump through hoops to
    "make it work" (e.g. for RE special characters).
    Would be interesting to benchmark a simple eq in a loop versus the
    concatenated or'ed REs. My gut feeling is that the loop with eq will be much
    faster

    jue
     
    Jürgen Exner, Oct 14, 2005
    #13
  14. Shashank Khanvilkar

    Dr.Ruud Guest

    Jürgen Exner schreef:

    > Perl has a perfectly fine eq operator for comparing strings. There is
    > no need to launch the big and expensive RE gun


    OK, but that argument vaporizes if the RE was or will be loaded for
    other parts of the program anyway.

    > and then jump through
    > hoops to "make it work" (e.g. for RE special characters).


    No need to jump through hoops:

    if ($a =~ m/\A(apple
    |banana
    |orange
    )
    \z/x) {
    (untested)


    > Would be interesting to benchmark a simple eq in a loop versus the
    > concatenated or'ed REs. My gut feeling is that the loop with eq will
    > be much faster


    Compiled simple regexes are also very fast. I'll try benchmark later.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Oct 14, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    2
    Views:
    27,943
    Phil Hanna
    Sep 20, 2003
  2. grz02

    Java String comparison

    grz02, Feb 23, 2005, in forum: Java
    Replies:
    40
    Views:
    15,847
    nooobody
    Mar 5, 2005
  3. Replies:
    21
    Views:
    1,453
    Alex Vinokur
    Aug 18, 2007
  4. Smithers
    Replies:
    12
    Views:
    1,208
    Ben Voigt [C++ MVP]
    Jul 7, 2009
  5. Deepu
    Replies:
    1
    Views:
    267
    ccc31807
    Feb 7, 2011
Loading...

Share This Page