my $best_score = 9999; my $best_combo; my @list = qw(1 1 1 2 3 4 4 4 5 5 6 6 6 7 7 8 8 8); while (my $combo = get_combo(\@list)) { # hard to do well?? $score = get_score($combo); # easy if ($score < $best_score) { $best_score = $score; $best_combo = $combo; } } print $best_score.': '.Dumper($best_combo); #### $VAR1 = [ [qw(1 1 1 2)], [qw(3 4 4 4)], [qw(5 5 )], [qw(6 6 6)], [qw(7 7)], [qw(8 8 8 9)], ];