#!/usr/bin/perl use strict; use warnings; # the diamond operator is special: # supply filenames on commandline or provide input on STDIN and it just works my @word = <>; chomp @word; my %in_list; undef @in_list{ @word }; # now all entries from @word are keys in %in_list for( @word ) { my @hook = grep { exists $in_list{ $_ } } ( substr( $_, 1 ), substr( $_, 0, length( $_ ) - 1 ), ); print "$_: @hook\n" if @hook; }