#! /usr/bin/perl chomp ((undef, my @D) = ); (my $Q = pop @D) =~ s/ /\\b.+\\b/; sub xt { grep s/\b(\d+),\1\b/$1/, map qq/$_[0],$_/, @_ } sub tx { print,exit for grep /^$Q$/, @_; map {xt $_, @_} @_ } push @D, tx @D for \(@D); __DATA__ 5 6 1 2 2 3 2 4 4 5 1 3 3 5 1 5