equality test for multiple variables

Discussion in 'Perl Misc' started by Matt Garrish, Jul 6, 2004.

  1. Matt Garrish

    Matt Garrish Guest

    Just curious if anyone has a more elegant way to check whether multiple
    values are equal. For example, given:

    $a = $b = $c = $d = $e = 'test';

    is there anything better than:

    if ( ($a eq $b) && ($b eq $c) && ($c eq $d) && ($d eq $e) ) { ... }

    It would be nice (cleaner) if you could simply test if $a eq $b eq $c eq $d,
    but that obviously won't work.

    Matt
    Matt Garrish, Jul 6, 2004
    #1
    1. Advertising

  2. Matt Garrish

    Uri Guttman Guest

    >>>>> "MG" == Matt Garrish <> writes:

    MG> Just curious if anyone has a more elegant way to check whether multiple
    MG> values are equal. For example, given:

    MG> $a = $b = $c = $d = $e = 'test';

    MG> is there anything better than:

    MG> if ( ($a eq $b) && ($b eq $c) && ($c eq $d) && ($d eq $e) ) { ... }

    MG> It would be nice (cleaner) if you could simply test if $a eq $b eq
    MG> $c eq $d, but that obviously won't work.

    use List::Util ;

    min( $a, $b, $c, $d, $e ) == max( $a, $b, $c, $d, $e ) ;

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jul 6, 2004
    #2
    1. Advertising

  3. Matt Garrish

    Joe Smith Guest

    Uri Guttman wrote:

    >>>>>>"MG" == Matt Garrish <> writes:

    >
    >
    > MG> Just curious if anyone has a more elegant way to check whether multiple
    > MG> values are equal. For example, given:
    >
    > MG> $a = $b = $c = $d = $e = 'test';
    >
    > MG> is there anything better than:
    >
    > MG> if ( ($a eq $b) && ($b eq $c) && ($c eq $d) && ($d eq $e) ) { ... }
    >
    > MG> It would be nice (cleaner) if you could simply test if $a eq $b eq
    > MG> $c eq $d, but that obviously won't work.
    >
    > use List::Util ;
    >
    > min( $a, $b, $c, $d, $e ) == max( $a, $b, $c, $d, $e ) ;


    I expect that you meant to post this instead:

    minstr( $a, $b, $c, $d, $e ) eq maxstr( $a, $b, $c, $d, $e ) ;

    -Joe
    Joe Smith, Jul 6, 2004
    #3
  4. > > use List::Util ;
    > >
    > > min( $a, $b, $c, $d, $e ) == max( $a, $b, $c, $d, $e ) ;

    >
    > I expect that you meant to post this instead:
    >
    > minstr( $a, $b, $c, $d, $e ) eq maxstr( $a, $b, $c, $d, $e ) ;
    >


    You really think this is more elegant? And, of course, this is fast...
    Eugene Mikheyev, Jul 6, 2004
    #4
  5. Matt Garrish

    gnari Guest

    "Matt Garrish" <> wrote in message
    news:J_pGc.14982$...
    > Just curious if anyone has a more elegant way to check whether multiple
    > values are equal. For example, given:
    >
    > $a = $b = $c = $d = $e = 'test';
    >
    > is there anything better than:
    >
    > if ( ($a eq $b) && ($b eq $c) && ($c eq $d) && ($d eq $e) ) { ... }
    >
    > It would be nice (cleaner) if you could simply test if $a eq $b eq $c eq

    $d,
    > but that obviously won't work.


    if you know that the strings do not contain say, ':', you can use:

    if ("$a:"x4 eq "$b:$c:$d:$e:") {...}

    gnari
    gnari, Jul 6, 2004
    #5
  6. Matt Garrish

    Anno Siegel Guest

    Eugene Mikheyev <> wrote in comp.lang.perl.misc:
    > > > use List::Util ;
    > > >
    > > > min( $a, $b, $c, $d, $e ) == max( $a, $b, $c, $d, $e ) ;

    > >
    > > I expect that you meant to post this instead:
    > >
    > > minstr( $a, $b, $c, $d, $e ) eq maxstr( $a, $b, $c, $d, $e ) ;
    > >

    >
    > You really think this is more elegant? And, of course, this is fast...


    Elegant? It's about correct vs. incorrect.

    Anno
    Anno Siegel, Jul 6, 2004
    #6
  7. > Elegant? It's about correct vs. incorrect.

    Don't get me wrong. One of them has mistyped, other one has corrected his
    mstake, but in general, my statement was something like "this test for
    equality is even less <elegant> as Matt's"
    Eugene Mikheyev, Jul 6, 2004
    #7
  8. Matt Garrish

    Anno Siegel Guest

    Matt Garrish <> wrote in comp.lang.perl.misc:
    > Just curious if anyone has a more elegant way to check whether multiple
    > values are equal. For example, given:
    >
    > $a = $b = $c = $d = $e = 'test';
    >
    > is there anything better than:
    >
    > if ( ($a eq $b) && ($b eq $c) && ($c eq $d) && ($d eq $e) ) { ... }
    >
    > It would be nice (cleaner) if you could simply test if $a eq $b eq $c eq $d,
    > but that obviously won't work.


    A hash can do it:

    my %h;
    @h{ $a, $b, $c, $d, $e} = ();
    if ( keys %h == 1 ) { ... }

    This doesn't need an external module, but Uri's suggestion using List::Util
    is about twice as fast.

    Anno
    Anno Siegel, Jul 6, 2004
    #8
  9. Matt Garrish

    Joe Smith Guest

    Eugene Mikheyev wrote:

    >>>use List::Util ;
    >>>
    >>>min( $a, $b, $c, $d, $e ) == max( $a, $b, $c, $d, $e ) ;

    >>
    >>I expect that you meant to post this instead:
    >>
    >>minstr( $a, $b, $c, $d, $e ) eq maxstr( $a, $b, $c, $d, $e ) ;

    >
    > You really think this is more elegant? And, of course, this is fast...


    No, I was pointing out that the OP use 'eq' (to compare strings) as
    opposed to '==' (to compare numbers), therefor min()==max() was not
    the correct answer.
    -Joe
    Joe Smith, Jul 6, 2004
    #9
  10. Matt Garrish

    Matt Garrish Guest

    "Anno Siegel" <-berlin.de> wrote in message
    news:cce00t$4hm$-Berlin.DE...
    > Matt Garrish <> wrote in comp.lang.perl.misc:
    > > Just curious if anyone has a more elegant way to check whether multiple
    > > values are equal. For example, given:
    > >
    > > $a = $b = $c = $d = $e = 'test';
    > >
    > > is there anything better than:
    > >
    > > if ( ($a eq $b) && ($b eq $c) && ($c eq $d) && ($d eq $e) ) { ... }
    > >
    > > It would be nice (cleaner) if you could simply test if $a eq $b eq $c eq

    $d,
    > > but that obviously won't work.

    >
    > A hash can do it:
    >
    > my %h;
    > @h{ $a, $b, $c, $d, $e} = ();
    > if ( keys %h == 1 ) { ... }
    >
    > This doesn't need an external module, but Uri's suggestion using

    List::Util
    > is about twice as fast.
    >


    Thanks to everyone for the suggestions! It's code I'm hoping not to be
    responsible for for long so the cleaner (clearer) it is to read the
    better... : )

    Matt
    Matt Garrish, Jul 6, 2004
    #10
  11. Matt Garrish

    Anno Siegel Guest

    Matt Garrish <> wrote in comp.lang.perl.misc:
    >
    > "Anno Siegel" <-berlin.de> wrote in message
    > news:cce00t$4hm$-Berlin.DE...
    > > Matt Garrish <> wrote in comp.lang.perl.misc:
    > > > Just curious if anyone has a more elegant way to check whether multiple
    > > > values are equal. For example, given:
    > > >
    > > > $a = $b = $c = $d = $e = 'test';
    > > >
    > > > is there anything better than:
    > > >
    > > > if ( ($a eq $b) && ($b eq $c) && ($c eq $d) && ($d eq $e) ) { ... }
    > > >
    > > > It would be nice (cleaner) if you could simply test if $a eq $b eq $c eq

    > $d,
    > > > but that obviously won't work.

    > >
    > > A hash can do it:
    > >
    > > my %h;
    > > @h{ $a, $b, $c, $d, $e} = ();
    > > if ( keys %h == 1 ) { ... }
    > >
    > > This doesn't need an external module, but Uri's suggestion using

    > List::Util
    > > is about twice as fast.
    > >

    >
    > Thanks to everyone for the suggestions! It's code I'm hoping not to be
    > responsible for for long so the cleaner (clearer) it is to read the
    > better... : )


    In that case, if you're using one of the less obvious solutions,
    make it a named sub. Here is a grep-based one:

    sub all_equal { not ( @_ and grep $_[ 0] ne $_, @_) }

    That one's a little faster than the hash, but still 15% slower than
    the List::Util solution.

    Anno
    Anno Siegel, Jul 6, 2004
    #11
  12. Matt Garrish

    Peter Scott Guest

    In article <J_pGc.14982$>,
    "Matt Garrish" <> writes:
    >Just curious if anyone has a more elegant way to check whether multiple
    >values are equal. For example, given:
    >
    >$a = $b = $c = $d = $e = 'test';
    >
    >is there anything better than:
    >
    >if ( ($a eq $b) && ($b eq $c) && ($c eq $d) && ($d eq $e) ) { ... }
    >
    >It would be nice (cleaner) if you could simply test if $a eq $b eq $c eq $d,
    >but that obviously won't work.


    use Quantum::Superpositions;
    if ( all($a, $b, $c, $d) eq $e ) ...

    Couldn't resist... :)

    --
    Peter Scott
    http://www.perldebugged.com/
    *** NEW *** http://www.perlmedic.com/
    Peter Scott, Jul 6, 2004
    #12
    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. Neroku
    Replies:
    12
    Views:
    594
    Patricia Shanahan
    Jan 27, 2007
  2. Alan Woodland

    Compile time equality test

    Alan Woodland, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    319
    Jim Langston
    Jul 10, 2007
  3. noydb

    test for list equality

    noydb, Dec 15, 2011, in forum: Python
    Replies:
    18
    Views:
    319
    Ian Kelly
    Dec 16, 2011
  4. Phrogz
    Replies:
    1
    Views:
    103
    Phrogz
    Dec 24, 2006
  5. 12 34
    Replies:
    15
    Views:
    189
    Peña, Botp
    Jul 3, 2007
Loading...

Share This Page