#!/usr/bin/perl -w use strict; open(LIST, "wordlist.txt") or die "$!"; my @wordlist = map { chomp; $_ } ; close LIST; open(MSTR, "master.txt") or die "$!"; my @master = sort {length $b <=> length $a} map { chomp; $_ } ; close MSTR; foreach my $word ( @master ) { my @matches; for (@wordlist) { next unless defined $_ and index($_, $word) >= 0; push @matches, $_; $_ = undef; } $word = [$word, join(", ",@matches)]; } print map {"$_->[0]: $_->[1].\n"} sort {$a->[0] cmp $b->[0]} @master;