Velocity Reviews > Perl > equality test for multiple variables

# equality test for multiple variables

Matt Garrish
Guest
Posts: n/a

 07-06-2004
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

Uri Guttman
Guest
Posts: n/a

 07-06-2004
>>>>> "MG" == Matt Garrish <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Joe Smith
Guest
Posts: n/a

 07-06-2004
Uri Guttman wrote:

>>>>>>"MG" == Matt Garrish <(E-Mail Removed)> 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

Eugene Mikheyev
Guest
Posts: n/a

 07-06-2004
> > 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...

gnari
Guest
Posts: n/a

 07-06-2004
"Matt Garrish" <(E-Mail Removed)> wrote in message
news:J_pGc.14982\$(E-Mail Removed). ..
> 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

Anno Siegel
Guest
Posts: n/a

 07-06-2004
Eugene Mikheyev <(E-Mail Removed)> 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

Eugene Mikheyev
Guest
Posts: n/a

 07-06-2004
> 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"

Anno Siegel
Guest
Posts: n/a

 07-06-2004
Matt Garrish <(E-Mail Removed)> 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

Anno

Joe Smith
Guest
Posts: n/a

 07-06-2004
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
-Joe

Matt Garrish
Guest
Posts: n/a

 07-06-2004

"Anno Siegel" <(E-Mail Removed)-berlin.de> wrote in message
news:cce00t\$4hm\$(E-Mail Removed)-Berlin.DE...
> Matt Garrish <(E-Mail Removed)> 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