#!/usr/bin/perl -w use strict; use warnings; use Algorithm::Combinatorics qw(combinations); my $rep = 10; my @data = 1..$rep; my $iter = combinations( \@data, 4 ); while( my $p = $iter->next ) { my ( $w, $x, $y, $z ) = @$p; next unless $w-2*$x+$y or $x-2*$y+$z; print "$w, $x, $y, $z\n"; }