#! perl -w $filename = "tryit.txt"; open(IN, $filename) || die; my %freq; my @title; # array of titles my $story; # number of current story while() { if(/^\<(.*)\>\s*$/) { # It's a title push @title, $1; $story = $#title; } elsif (defined $story) { # It's plain text s/[\.,:;\?"!\(\)\[\]\{\}(--)_]//g; foreach my $word (/\w+/g) { $freq{lc $word}[$story]++; } } } # print "\n\nOutput tab delimited text file:\n\n"; { local($\, $,) = ("\n", "\t"); print '', @title; foreach my $row (sort keys %freq) { print $row, map $_ || '', @{$freq{$row}}[0 .. @title-1] } } close IN