Mr_Noob said:
i have a text that contains nearly 30000 words.
I'd like to know the number of occurences of each word of this text
and sort the result in descending order.
Is this easily feasible using perl?
Very simple actually.
If so, how ? (i am a huge noob..)
Just split() the string into words and count each word pattern in a hash,
using the word pattern as the key and the counter as the value.
Then sort the keys of that hash into an array by order of their values.
And then print them.
my $unhelpfulFAQ =
' How can I count the number of occurrences of a substring within a
string?
There are a number of ways, with varying efficiency. If you want
a count of a certain single character (X) within a string, you
can use the "tr///" function like so:
$string = "ThisXlineXhasXsomeXxsXinXit";
$count = ($string =~ tr/X//);
print "There are $count X characters in the string";
This is fine if you are just looking for a single character.
However, if you are trying to count multiple character
substrings within a larger string, "tr///" wont work. What you
can do is wrap a while() loop around a global pattern match. For
example, lets count negative integers:
$string = "-9 55 48 -2 23 -76 4 14 -44";
while ($string =~ /-\d+/g) { $count++ }
print "There are $count negative numbers in the string";';
my %count;
for (split /\W+/, $unhelpfulFAQ){
$count{$_}++;
}
my @sorted = sort {
$count{$b} <=> $count{$a}
} keys %count;
for (@sorted) {
print "$_: \t $count{$_}\n";
}