Do you remember the word count challenge from BP.org? For those who don't a little explanation. The task was to count all and unique words in Bible and then count how many times each word appears in the whole text. Then the result should be sorted and written to the file. Here is a Pike version:
void main() {
array|mixed a, b, c, b2;
b = ({});
b2 = ({});
c = ({});
int x, x2;
string y;
write("Reading the file...\n");
string content=Stdio.File("Bible.txt")->read();
content = lower_case(content);
a = content / " ";
write("Counting all words...\n");
for(x = 0; x < (sizeof(a) - 1); x++) {
a[x] = Regexp.PCRE("[^a-z]")->replace(a[x]," ");
b = b + (a[x] / " ");
}
for(x = 0; x < (sizeof(b) - 1); x++) {
if (strlen(b[x]) > 1 || b[x] == "a" || b[x] == "i") {
c = c + ({b[x]});
}
}
object plik = Stdio.File();
plik -> open("Words.txt", "wc");
write("All words: ");
plik -> write("All words: ");
write("%d\n", sizeof(c));
plik -> write("%d\n", sizeof(c));
write("Counting unique words...\n");
plik -> write("Counting unique words...\n");
a = Array.uniq(c);
write("Unique words: ");
plik -> write("Unique words: ");
write("%d\n", sizeof(a));
plik -> write("%d\n\n", sizeof(a));
write("Counting numbers of occurences of each word and sorting the result...\n");
b = ({});
x = 0;
foreach (a, y) {
b = b + ({y});
b2 = b2 + ({Array.count(c, y)});
}
c = (b2[0 .. sizeof(b2)]);
c = reverse(sort(c));
for(x = 0; x < sizeof(c); x++) {
for(x2 = 0; x2 < sizeof(b2); x2++) {
if (b2[x2] == c[x]) {
write(b[x2]);
plik -> write(b[x2]);
write(" - ");
plik -> write(" - ");
write("%d\n", b2[x2]);
plik -> write("%d\n", b2[x2]);
b2[x2] = -1000;
}
}
}
plik -> close();
}
BTW That was a very nice challenge. Maybe we should think about bringing it back?