use Modern::Perl;
use Algorithm::Permute;
use List::AllUtils qw/uniq/;
find_perms(1151);
sub find_perms {
my ($value) = @_;
my @others;
my @digits = split(//, $value);
my $perm = Algorithm::Permute->new( \@digits );
while (my @next = $perm->next()) {
my $number = join('', @next);
push @others, $number;
}
@others = sort uniq @others;
# this one works correctly
# @others = sort ( uniq( @others ));
say "FOUND @others";
}
Output:
FOUND 1115 1115 1115 1115 1115 1115 1151 1151 1151 1151 1151 1151 1511 1511 1511 1511 1511 1511 5111 5111 5111 5111 5111 5111
Hi,
After discovering out that Algorithm::Permute is producing duplicates, most likely due to the amount of '1's in "1151" i decided to use uniq
. However using sort uniq
without the parenthesis doesn't produce expected results. But sort(uniq(@x))
does. What gives?
perldoc -f sort
lists three syntaxes for the sort
function:
sort SUBNAME LIST
sort BLOCK LIST
sort LIST
sort uniq @others
matches the sort SUBNAME LIST
syntax of sort. It expects uniq
to be a function that compares the global variables $a
and $b
, and returns <0
, 0
, or >0
to indicate the relative ordering of $a
and $b
.
It looks like you were expecting and wanting the sort LIST
syntax, which is what you get when you say one of
sort(uniq(@others))
sort(uniq @others)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With