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 - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
| [reply] [d/l] |
|
if ( @list == grep { $_ == $list[0] } @list ) {
&do_something;
}
... seems clearer to me. But then again, that's just me ;)
[ ar0n ]
| [reply] [d/l] |
|
if ( !grep $_ != $list[0], @list ) {
&do_something;
}
... probably clearer, definitely probably faster :)
MeowChow
s aamecha.s a..a\u$&owag.print | [reply] [d/l] |
|
Thank you for the suggesting code... it works well without using temporary arrays/hashes.
By the way, what does TIMTOWODI mean?
| [reply] |
|
TIMTOWTDI - There Is More Than One Way To Do It
TIMTOWODI - There Is More Than One Way Of Doing It
| [reply] |
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. | [reply] |
|
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.
| [reply] |
|
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.
| [reply] [d/l] [select] |
|
| [reply] |
|
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?
| [reply] |
|
|
|
|
|
See my answer further down, but call the function like so:
all_equal_ints(scalar(@array1), scalar(@array2))
| [reply] [d/l] |
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.
| [reply] [d/l] |
|
I was about to do that, however the TIMTOWTDI / TIMTOWODI spirit hinted me to ask here.
By the way, what's 'baby Perl'??
| [reply] |
|
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.
| [reply] |
|
|
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;
}
| [reply] [d/l] |
|
#!/usr/local/bin/perl -w
use strict;
my @list=("abcd123","abcd143","abcd123","abcd123");
$_=join("",@list);
s/$list[0]//g;
print "not equal\n" if ($_);
| [reply] [d/l] |
|
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.
| [reply] [d/l] |
|