Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#!/usr/bin/perl
use warnings;
use strict;
my @a = (1,2,3);
my @b = (3,1,2);
my $result = 'true';
{
my @c = sort @a;
my @d = sort @b;
# Check for same length
$result = 'false', last if($#a != $#b);
for(my $i=0; $i<=$#a; $i++) {
# WARNING: Numeric test below. Assuming all numeric entries.
$result = 'false', last unless($c[$i] == $d[$i]);
}
}
# Here result is 'true' if they match or 'false' otherwise
print $result, "\n";
use List::Permutor;
my @array = (2,1,3);
my @array1 = (1,2,3);
my $perm = new List::Permutor @array;
while (my @set = $perm->next) {
if ("@array1" eq "@set") {
print "\@array1 is a permuation of \@array: @array\n";
last;
}
}
The sort order doesn't matter, as long as the same sort order is applied to both arrays. However, the join character would need to be unique, guaranteed not to be present in either array.
Internally the comparator compares the two arrays by using join to turn both arrays into strings and comparing the strings using eq. In the joined strings, the elements of the original arrays are separated with the ^G character. This can cause problems if your array data contains ^G characters as it is possible that two different arrays can be converted to the same string.