Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Fastest way to compare multiple variables?

by Sifmole (Chaplain)
on May 15, 2001 at 21:20 UTC ( #80626=note: print w/replies, xml ) Need Help??

in reply to Fastest way to compare multiple variables?

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.

  • Comment on Re: Fastest way to compare multiple variables?

Replies are listed 'Best First'.
Re: Re: Fastest way to compare multiple variables?
by Anonymous Monk on May 15, 2001 at 21:27 UTC
    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?
        use CGI; @array1 = param('datalist1'); @array2 = param('datalist2'); @array3 = param('datalist3');
        If you could make that instead:
        my %data = map { $_ => [param $_] } qw(datalist1 datalist2 datalist3);
        Then we can compare their lengths with:
        sub compare { my @lengths = map { scalar @{$data{$_}} } qw(datalist1 datalist2 dat +alist3); my $first = shift @lengths; $first == $_ or return 0 for @lengths; return 1; }
        See how much easier? Regularity in variable names is almost always a sign that they should be part of a larger structure instead.

        -- Randal L. Schwartz, Perl hacker

      See my answer further down, but call the function like so:
      all_equal_ints(scalar(@array1), scalar(@array2))

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://80626]
[Corion]: Oh yay. The (external, not guided by me) programmers have chosen Moose+DBIx::Class for some implementation, and now seem to do 1+n SELECT statements for each row, as is usual when using ORMs.
[Corion]: So maybe I should investigate how to plug in a cache in front of DBIx::Class so I can do a ->selectall_hashre f and then satisfy the "sub"-selects from that cached single SELECT statement ...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2017-09-25 10:57 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (279 votes). Check out past polls.