miracle of perl
Friday, April 29th, 2005PS. You may not understand if you have never programmed seriously in conventional languages.
Task:
Write a program which reads in a series of text, and create a list of all words in the input text and display its word count in alphabetical order.
Conventional Procedure:
- Create 2 coresponding arrays, one contains the word name while the other contain the word count.
- Store the input string into the buffer, extract each word out using string manupulator at the break of a space bar, store in the word name array and increase coresponding word count by one.
- While extracting each word, check if it was already noted in the array and increase its corresponding word count.
- Perform a quicksort with the arrays and display the results with iteratives.
Solution:
#!/usr/bin/perl
my %dict;
while (<>) {
chomp;
foreach (split) {
$dict{$_}++;
}
}my $i = 0;
foreach (sort keys %dict) {
print $i++, " $_:", $dict{$_}, "\n";;
}
(Note: No, I didn’t write it. My bf did.)