foreach $line ( @theseLines ) { $count++; $line = lc $line; $line =~ s/[.,:;?!]//g; while ( $line =~ /(\p{Alnum}+([-']\p{Alnum}+)*)/g ) { $word = $&; if ( $word =~ /\s/ || $word eq "") { next } $Count{$word}++; if ( defined $Line{$word} ) { $Line{$word} =~ m/(\d*?)$/; if ( $1 == $count ) { next; } else { $Line{$word} .= ", $count"; } } else { $Line{$word} = $count; } } }

@theseWords = keys %Line; @theseWords = sort @theseWords; foreach $word ( @theseWords ) { my @lines = split m/,/, $Line{$word}; for (@lines){ print ("$word, $_"); } }