my %words; ++$words{$_} for /\w+/g; for ( [qw( Fred Flinstone )], [qw( Barney Rubble )], [qw( Joe Rockhead )], ) { next if @$_ != grep $words{$_}, @$_; print("matched @$_\n"); } #### my %next_words; $next_words{$1} = $2; while /(\w+)\s+(?=\w+)/g; for ( [qw( Fred Flinstone )], [qw( Barney Rubble )], [qw( Joe Rockhead )], ) { next if @$_ != grep defined($next_words{$_}), @$_; print(join(' ', map "$_ $next_words{$_}", @$_), "\n"); }