#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11108762 use warnings; $_ = <<''; 1 2 3 4 1 2 3 5 1 2 4 6 1 2 3 4 1 2 3 5 1 2 4 7 4 6 3 9 my (@match, @nomatch); while( /^(?=((?:.*\n){5})(.*\n))/gm ) { my ($five, @six) = ($1, sort split ' ', $2); my %found = map +($_ => 1), split ' ', $five; push @match, "@{[ grep $found{$_}, @six ]}\n"; push @nomatch, "@{[ grep !$found{$_}, @six ]}\n"; } print "match:\n", @match, "\nnomatch:\n", @nomatch;