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 sortlists three syntaxes for thesortfunction:sort uniq @othersmatches thesort SUBNAME LISTsyntax of sort. It expectsuniqto be a function that compares the global variables$aand$b, and returns<0,0, or>0to indicate the relative ordering of$aand$b.It looks like you were expecting and wanting the
sort LISTsyntax, which is what you get when you say one of