I have been trying to sort words based on word count and I have done so but I am having problems.
When I sort them into order, it seems that the word count is reset to 1 for all words?
Does anyone why this is?
(oh yeah and the regular expression are not all perfect they are to eliminate non-words but I havent got round to completing that part).
use CGI qw(:all);
use LWP::Simple qw(get);
$text = "sign-writer been been 'twas up-to-date 'twas has- been <!-- comment --> <here> Assignment1 £Assignment As£ment ";
print "<xmp>$text</xmp >", br(), "\n";
$text = lc($text);
$text =~ s/<(.*?)>//g;
$text =~ s/-{2,}/ /gi;
$text =~ s/[^a-zA-Z0-9\'\-\_]/ /g;
my @array = split(/\s+/,$text);
%count = ();
foreach $string ( @array ) {
$count { $string }++;
}
foreach $key ( keys %count ) {
$value = $count { $key }, br (), "\n";
}
print "<table border='2'>";
print "<th>String</th><th>Count</th>";
foreach $key ( sort {$count{$b} <=> $count{$a}} keys %count)
{
print "$key $value\n";
}

New Topic/Question
Reply


MultiQuote





|