Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Fastest way to compare multiple variables?

by runrig (Abbot)
on May 16, 2001 at 04:50 UTC ( #80763=note: print w/replies, xml ) Need Help??


in reply to Fastest way to compare multiple variables?

'Fastest' depends on whether or not they ARE 'all equal' :)
#!/usr/bin/perl my $a = 2; my $b = 2; my @arr = qw( 2 2 2 ); if (all_equal_ints($a, $b, @arr)) { print "They're all equal!\n" } else { print "They're not all equal!\n"; } sub all_equal_ints { my $first = shift; for (@_) { return 0 unless $first == $_; } return 1; }

Replies are listed 'Best First'.
Re: Re: Fastest way to compare multiple variables?
by zeidrik (Scribe) on May 16, 2001 at 11:58 UTC
    Try this:
    #!/usr/local/bin/perl -w use strict; my @list=("abcd123","abcd143","abcd123","abcd123"); $_=join("",@list); s/$list[0]//g; print "not equal\n" if ($_);
      The problem with that is that the code will produce an incorrect result in the case:

      my @list={"a","aaaa","aa","aaaaaaaa","a"};

      In the above (admittedly pathological) case, it would report all equal when this is not true.

        A comment correcting a code snippet that was posted 12 years ago.

        I love Perlmonks. ;-)

        Christopher Cashell

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://80763]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2016-10-01 23:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (9 votes). Check out past polls.