Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Re: Fastest way to compare multiple variables?

by Anonymous Monk
on May 15, 2001 at 21:27 UTC ( #80632=note: print w/ replies, xml ) Need Help??


in reply to Re: Fastest way to compare multiple variables?
in thread Fastest way to compare multiple variables?

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.


Comment on Re: Re: Fastest way to compare multiple variables?
Re: Re: Re: Fastest way to compare multiple variables?
by merlyn (Sage) on May 15, 2001 at 21:32 UTC
    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

Re: Re: Re: Fastest way to compare multiple variables?
by arturo (Vicar) on May 15, 2001 at 21:34 UTC

    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.

    HTH.

Re: Re: Re: Fastest way to compare multiple variables?
by Anonymous Monk on May 15, 2001 at 21:48 UTC
    Wow, the light at perlmonks.com 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

        I see... thanks. Seem function map is pretty useful.
        Okay, I cannot resist. This is the product of not enough sleep and I warn all who continue. Given we have used merlyn's suggestion for a hash of arrays ( I was actually thinking of an array of ararys, but why not a hash? ), what if we did something like this
        sub compare { my %data = @_; my @lengths = sort { $a <=> $b } map { scalar@{$data{$_}} } keys %da +ta; return $lengths[0] == $lengths[-1]; }
        Basically, if we sort the lengths and the last element is equal to the first element, then everything else inbetween must be equal.

        This has likely no value - it really isn't clearer nor does it likely save any cycles. I just thought it was fun and have not had enough sleep.

        UPDATE: did I mention not enough sleep? Fixed a typo
        mikfire

        I too couldn't resist. A single walk through the hash's keys is the fastest way I could think of doing this:

        sub dkubb_compare { my $data = shift; my $first = @{$data{scalar each %$data}}; while(my $key = scalar each %$data) { return 0 unless $first == @{$data{$key}}; } return 1; }

        IMHO, it's a little too obscure to be used in production code, but I think it still is a neat idiom.

Re: Re: Re: Fastest way to compare multiple variables?
by runrig (Abbot) on May 16, 2001 at 04:53 UTC
    See my answer further down, but call the function like so:
    all_equal_ints(scalar(@array1), scalar(@array2))

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-09-20 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls