Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Re: Re: Fastest way to compare multiple variables?

by merlyn (Sage)
on May 15, 2001 at 21:51 UTC ( #80645=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re: Re: Re: Re: Fastest way to compare multiple variables?
Select or Download Code
Re: Re: Re: Re: Re: Fastest way to compare multiple variables?
by Anonymous Monk on May 15, 2001 at 22:05 UTC
    I see... thanks. Seem function map is pretty useful.
Re: Re: Re: Re: Re: Fastest way to compare multiple variables?
by mikfire (Deacon) on May 15, 2001 at 23:10 UTC
    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

(dkubb) Re: (5) Fastest way to compare multiple variables?
by dkubb (Deacon) on May 18, 2001 at 12:22 UTC

    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-11-23 21:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls