Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Fastest way to compare multiple variables?

by Anonymous Monk
on May 15, 2001 at 21:10 UTC ( #80620=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Is there anyway (without using hashes) to do something like

if ($a == $b == $c == $d) {

What I have: 15-20 variables ALREADY have their values (integers)
What I need: tell if all those variables have a same value or not
What I know: there are already ways to do that by using arrays/hashes.

Or, hash is still the fastest way? Thank you...
  • Comment on Fastest way to compare multiple variables?

Replies are listed 'Best First'.
Re: Fastest way to compare multiple variables?
by Masem (Monsignor) on May 15, 2001 at 21:14 UTC
    if ( !grep { $_ != $a } ($b, $c, $d, $e...) ) { &do_something }

    Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain
      if ( @list == grep { $_ == $list[0] } @list ) { &do_something; }
      ... seems clearer to me. But then again, that's just me ;)

      ar0n ]

        if ( !grep $_ != $list[0], @list ) { &do_something; }
        ... probably clearer, definitely probably faster :)
                       s aamecha.s a..a\u$&owag.print
      Thank you for the suggesting code... it works well without using temporary arrays/hashes.

      By the way, what does TIMTOWODI mean?
        TIMTOWTDI - There Is More Than One Way To Do It
        TIMTOWODI - There Is More Than One Way Of Doing It
Re: Fastest way to compare multiple variables?
by Sifmole (Chaplain) on May 15, 2001 at 21:20 UTC
    Depending on how the variables are being created, it might make sense to perform the check as the variables are being filled -- retaining last value set, and comparing it to the value you are going to be setting.

    This might allow you to know earlier that you need to take the alternate path; and depending on the requirements of that path allow you to avoid performing unneccessary work.

      For this time, I need to compare if sizes of 15-20 arrays are the same or not. Those 15-20 arrays are almost created at the same time (during file readin/phrasing). Any new ideas?

      Thank you for your suggestion anyway. I believe it's useful on other situations (so that my script won't be full of @tmp_array or %tmp_hash.

        Here's one way : cache the size of the first array created and set a variable $allsamesize to have a true value (=1 will do). Then check each new array's size against the cached value and, if it's not the same, set $allsamesize to 0.


        It would help to know how the arrays are named. Are they all part of a larger data structure? Can you iterate over the list of arrays?

        -- Randal L. Schwartz, Perl hacker

        Wow, the light at seems to be ON all day/night! Someone is always here... cool!

        All Those arrays are actually from a CGI page:

        use CGI;
        @array1 = param('datalist1');
        @array2 = param('datalist2');
        @array3 = param('datalist3');

        I think Masem's idea works well already. Or any suggestions/comments for learning purpose?
        See my answer further down, but call the function like so:
        all_equal_ints(scalar(@array1), scalar(@array2))
Re: Fastest way to compare multiple variables?
by sierrathedog04 (Hermit) on May 15, 2001 at 22:03 UTC
    if (($a == $b) && ($a == $c) && ($a == $d) ... ){ &do_something }
    Update: There seems to be no way to solve this problem without listing each variable by name, since we do not know the names of the variables. So listing them and doing simple equality tests is as maintainable and efficient as other approaches.

    A solution that one writes in baby Perl can be just as valid as any other, in the same way that the lyrics to Louie, Louie were as great as those of Dylan's Mr. Tambourine Man, and the Ramones (R.I.P. Joey) were as great as the Beatles.

      I was about to do that, however the TIMTOWTDI / TIMTOWODI spirit hinted me to ask here.

      By the way, what's 'baby Perl'??
        I would define "baby Perl" as any Perl that one can write using only the constructs set forth in the first chapter of Merlyn (Randal L. Schwartz)'s estimable Lhama book "Learning Perl." Thus, one could use print, if, chomp, <STDIN>and even qw and tr but not grep, map, hashes, typeglobs, references and the flipflop operator.

        I say, if you know baby Perl, you can do almost anything, just as if you know a thousand words of French and the present tense conjugations of verbs you can get by in Paris.

Re: Fastest way to compare multiple variables?
by runrig (Abbot) on May 16, 2001 at 04:50 UTC
    '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; }
      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://80620]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-10-15 08:39 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (82 votes). Check out past polls.