Quicksilver has asked for the wisdom of the Perl Monks concerning the following question:
What I'd like to get, if "Mary had a little lamb" was on line 8 and I'm searching in little, is: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; } } } <br /> <br /> @theseWords = keys %Line; @theseWords = sort @theseWords; foreach $word ( @theseWords ) { my @lines = split m/,/, $Line{$word}; for (@lines){ print ("$word, $_"); } }
|
|---|