diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2011-10-14 04:55:05 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2011-10-14 04:55:05 +0200 |
commit | 6aa0e0017d7d0cddc006da885946934b06949a91 (patch) | |
tree | 66b688ec32e2f91266db760b1762f2a50cc52036 /libslang/examples/assoc.sl | |
parent | a966db5b71328f6adf9dd767e64b322a3bd7ed9c (diff) | |
download | erlang-slang-fork-6aa0e0017d7d0cddc006da885946934b06949a91.tar.gz |
include libslang-1.4.9 and automatically build it and link erlang-slang against it
few (erlang) people will still have libslang-1.4.9 installed or spend time
to get it to link against the driver
Diffstat (limited to 'libslang/examples/assoc.sl')
-rw-r--r-- | libslang/examples/assoc.sl | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/libslang/examples/assoc.sl b/libslang/examples/assoc.sl new file mode 100644 index 0000000..c4b2b37 --- /dev/null +++ b/libslang/examples/assoc.sl @@ -0,0 +1,46 @@ +% This example illustrates the use of associative arrays. +% The function 'analyse_file' counts the number of occurrences of each word +% in a specified file. Once the file has been read in, it writes out +% the list of words and number of occurrences to the file counts.log + +define analyse_file (file) +{ + variable fp; + variable line; + variable i, a, n, word; + variable keys, values; + + fp = fopen (file, "r"); + if (fp == NULL) + verror ("Unable to open %s", file); + + % Create an Integer_Type assoc array with default value of 0. + a = Assoc_Type[Integer_Type, 0]; + + while (-1 != fgets (&line, fp)) + { + foreach (strtok (strlow(line), "^a-zA-Z\d128-\d255")) + { + word = (); + a[word] = a[word] + 1; % default value of 0 assumed!! + } + } + + () = fclose (fp); + keys = assoc_get_keys (a); + values = assoc_get_values (a); + + i = array_sort (values); + keys = keys[i]; + values = values[i]; + + fp = fopen ("count.log", "w"); + % The default array_sort for Int_Type is an ascending sort. We want the + % opposite. + for (i = n-1; i >= 0; i--) + { + () = fputs (sprintf ("%s:\t%d\n", keys[i], values[i]), fp); + } + () = fclose (fp); +} + |