my @aOfA=(); for ( my $i=0; $i<=$#nlist; $i++ ) { my @tmp = (); ( $i > 0 ) and (($nlist[$i] - $nlist[$i-1]) < 2 ) and $tmp[0]=$nlist[$i-1]; push @tmp, $nlist[$i]; ( $i < $#nlist ) and (($nlist[$i+1] - $nlist[$i]) < 2 ) and push @tmp, $nlist[$i+1]; push @aOfA, \@tmp; # Note that @tmp turns anonymous but doesn't dispose # at this reloop point }