#!/usr/bin/perl -w use strict; use vars qw(@array); @array = qw( hey foo bar ); combinations(scalar(@array), [], \@array); sub combinations { my($count,$sofar,$remaining)=@_; if (!$count) { print join(" ",@$sofar),"\n"; return; } combinations($count-1,[@$sofar],[@$remaining]); foreach my $r (@$remaining) { combinations($count-1,[@$sofar, $r], [@$remaining]); } }