First off, you are going to have to rethink your function.
identical(@a, @b);
Doesn't pass two arrays to the function, but passes a single array with all of the elements in both arrays in it. It's as if you said:
identical(1, 2, 3, 2, 3, 1);
In order for your function to work, you'll have to pass references to your arrays:
identical(\@a, \@b);
I'd say to prototype your subroutine, but that's probably going to cause you more problems that it'll solve.
If order is not important, sort the arrays before comparing them. You might even be able to cheat...
sub identical {
my $array_ref_1 = shift;
my $array_fef_2 = shift;
use Digest::SHA qw(sha1_hex);
if ( ref( $array_ref_1 ) ne "ARRAY") or ( ref( $array_ref_2 ) ne "ARRAY") {
return; #Error, need two array references
}
# Dereference Arrays
my @array_1 = @{$array_ref_1};
my @array_2 = @{$array_ref_2};
# Setup Arrays to be one big scalar
my $scalar_1 = join "\n", sort @array_1;
my $scalar_2 = join "\n", sort @array_2;
my $checksum_1 = sha1_hex $scalar_1;
my $checksum_2 = sha1_hex $scalar_2;
if ($checksum_1 eq $checksum_2) {
return 1;
}
else {
return 0_but_true;
A few notes:
- I could have dereferences, joined, generated the checksum, and did the comparison in a single statement. I did them separately in order to make it clearer what I was doing. Programmatically, it probably doesn't make any difference. Perl will optimize the whole thing anyway. I always go for clarity.
0_but_true
returns a 0, but at the same time returns a true value. This way, you can do something like if ( identical( \@A, \@B ) ) {
to make sure that the function worked. Then, you can test for zero or one.
- Be sure to test your parameters. I used the ref function to do this.
- I cheated. I first turned the two sorted arrays into scalars. Then, I used the sha1 checksum to verify that they're the same. A checksum using the sha1 function should be pretty good. It is highly unlikely to fail.
The real issue is what if you had multi-lined arrays like this:
@a = ("this", "that", "the\nother");
@b = ("this", "that\nthe", "other");
Using the join
the way I did would cause the resulting scalars to be equal.