Two completely different subs, the first is much faster, the second is much more flexible. You need to replace @ary with your actual data :)
#!/usr/bin/perl
use warnings;
use strict;
my @ary = (0..5);
print map { "@$_\n" } pairs(@ary);
print "\nCHOOSE:\n";
print map { "@$_\n" } choose(2, @ary);
# hard-coded return of pairs
sub pairs {
my @result;
foreach my $i (0..$#_-1) {
foreach my $j ($i+1..$#_) {
push @result, [$_[$i], $_[$j]];
}
}
return @result;
}
#more general solution: choose any combinations of k elms from @_:
sub choose {
no warnings 'recursion';
my $k = shift;
my @ary = @_;
my $n = scalar @ary;
return if ($k > $n) || ($k < 1);
if ($k == 1) {
return map { [$_] } @ary;
}
my @AoA;
foreach my $i (0..($n-$k)) {
push @AoA, map { [$ary[$i], @{$_}] } choose($k-1, @ary[ $i+1..
+$#ary ]);
}
return @AoA;
}
s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print