use strict; # mustn't forget that open(LIST, "wordlist.txt"); open(MSTR, "master.txt"); # get the wordlist my @wordlist = map { chomp; $_ } ; # get the master list, sorted by word length, longest words first my @master = sort { length($b) <=> length($a) } map { chomp; $_ } ; # declare a hash to hold the findings: my %report; foreach my $lookfor ( @master ) { foreach my $lookat ( @wordlist ) { if ( $lookat =~ /$lookfor/ ) { $report{$lookfor} .= ",$lookat"; $lookat = ""; # erases this word from @wordlist } } } foreach my $word ( sort keys %report ) { $report{$word} =~ s/,/ /; # change initial comma to space print "$word:$report{$word}$/"; }