./1.permute.pl : permutations for 851: 8,5,1 8,1,5 5,8,1 5,1,8 1,8,5 1,5,8 $ cat 1.permute.pl #!/usr/bin/perl -w use 5.011; use Algorithm::Combinatorics qw/permutations/; die "Usage : $0 number" unless scalar @ARGV; my @digits = split //, $ARGV[0]; my $iter = permutations( \@digits ); print "$0 : permutations for $ARGV[0]:\n"; while ( my $p = $iter->next ) { print " " . join( ",", @$p ) . "\n"; } $ #### $ ./2.permute.pl 372 ./2.permute.pl : permutations for 372: 3,7,2 3,2,7 7,3,2 7,2,3 2,3,7 2,7,3 airdish is ARRAY(0x5621ec17ad00) ARRAY(0x5621ec17ae80) ARRAY(0x5621ec17aec8) ARRAY(0x5621ec23e770) ARRAY(0x5621ec2d4d80) ARRAY(0x5621ec2d5158) [[3, 7, 2], [3, 2, 7], [7, 3, 2], [7, 2, 3], [2, 3, 7], [2, 7, 3]] descending is [[2, 7, 3], [2, 3, 7], [7, 2, 3], [7, 3, 2], [3, 2, 7], [3, 7, 2]] $