equality test for multiple variables

M

Matt Garrish

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
 
U

Uri Guttman

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
 
J

Joe Smith

Uri said:
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
 
E

Eugene Mikheyev

use List::Util ;
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...
 
G

gnari

Matt Garrish said:
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
 
E

Eugene Mikheyev

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"
 
A

Anno Siegel

Matt Garrish said:
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
 
J

Joe Smith

Eugene said:
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
 
M

Matt Garrish

Anno Siegel said:
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
 
A

Anno Siegel

Matt Garrish said:
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
 
P

Peter Scott

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... :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top