aboutsummaryrefslogtreecommitdiffhomepage
path: root/libslang/examples/sort.sl
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2011-10-14 04:55:05 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2011-10-14 04:55:05 +0200
commit6aa0e0017d7d0cddc006da885946934b06949a91 (patch)
tree66b688ec32e2f91266db760b1762f2a50cc52036 /libslang/examples/sort.sl
parenta966db5b71328f6adf9dd767e64b322a3bd7ed9c (diff)
downloaderlang-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/sort.sl')
-rw-r--r--libslang/examples/sort.sl62
1 files changed, 62 insertions, 0 deletions
diff --git a/libslang/examples/sort.sl b/libslang/examples/sort.sl
new file mode 100644
index 0000000..c2eb7ad
--- /dev/null
+++ b/libslang/examples/sort.sl
@@ -0,0 +1,62 @@
+#! /usr/bin/env slsh
+
+% This program presents the solution to a problem posed by
+% Tom Christiansen <tchrist@mox.perl.com>. The problem reads:
+%
+% Sort an input file that consists of lines like this
+%
+% var1=23 other=14 ditto=23 fred=2
+%
+% such that each output line is sorted WRT to the number. Order
+% of output lines does not change. Resolve collisions using the
+% variable name. e.g.
+%
+% fred=2 other=14 ditto=23 var1=23
+%
+% Lines may be up to several kilobytes in length and contain
+% zillions of variables.
+%---------------------------------------------------------------------------
+%
+% The solution presented below works by breaking up the line into an
+% array of alternating keywords and values with the keywords as the even
+% elements and the values as the odd. It is about 30% faster than the
+% python solution.
+
+static variable Keys, Values;
+static define sort_fun (i, j)
+{
+ variable s, a, b;
+
+ s = Values[i] - Values[j];
+ !if (s)
+ return strcmp (Keys[i], Keys[j]);
+ return s;
+}
+
+
+define main ()
+{
+ variable line, len, i, vals;
+ foreach (stdin)
+ {
+ line = ();
+ line = strtok (line, " \t\n=");
+ len = length(line)/2;
+ if (len == 0)
+ continue;
+
+ % Even elements are keys, odd are values
+ Keys = line[[0::2]];
+ vals = line[[1::2]];
+
+ Values = array_map(Int_Type, &integer, vals);
+
+ i = array_sort ([0:len-1], &sort_fun);
+
+ % There are different ways of writing the result. Here is a
+ % fast way that avoids a loop.
+ () = printf ("%s\n", strjoin (Keys[i] + "=" + vals[i], " "));
+ }
+}
+
+main ();