[Beowulf] C vs C++ challenge (awk version)

Robert G. Brown rgb at phy.duke.edu
Thu Jan 29 18:22:08 EST 2004

On Thu, 29 Jan 2004, Selva Nair wrote:

> But this one does not count unique words, does it?

Hmmm.  I read "distinct words" as being words distinguished from one
another by separators, not as unique words.  As in "two and two" are
one line, three distinct words, but only two unique words.

I'll have to hack a bit tomorrow to make the C do only distinct words.
For that the hash table solution is probably optimal, but I might as
well see how much extra time the loops take.

> Here is the script:
> #!/bin/awk -f
> {
>   for(i = 1; i <= NF; i++) {
>     if (words[$i]) continue;
>     words[$i] = 1 ;
>     ++nwords;
>   }
> }
> END {
>   printf "Number of distinct words = %i\n", nwords;
> }

I always have liked awk;-)


Robert G. Brown	                       http://www.phy.duke.edu/~rgb/
Duke University Dept. of Physics, Box 90305
Durham, N.C. 27708-0305
Phone: 1-919-660-2567  Fax: 919-660-2525     email:rgb at phy.duke.edu

Beowulf mailing list, Beowulf at beowulf.org
To change your subscription (digest mode or unsubscribe) visit http://www.beowulf.org/mailman/listinfo/beowulf

More information about the Beowulf mailing list