From e802e65c7f8e79fd397e7ae0bcaa766d426b3f35 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Tue, 20 Oct 2020 10:18:57 +0200 Subject: [PATCH] Add pyramid script to sort lines by length --- bin/pyramid | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 bin/pyramid diff --git a/bin/pyramid b/bin/pyramid new file mode 100755 index 0000000..d359a9b --- /dev/null +++ b/bin/pyramid @@ -0,0 +1,27 @@ +#!/usr/bin/env lua + +local arrr = require 'arrr' + +local options = arrr { + { "Sort longest lines first", "--reverse", "-r" }; +} {...} + +local lines = {} + +for line in io.lines() do + table.insert(lines, {#line, line}) +end + +if options.reverse then + table.sort(lines, function(a, b) + return a[1] > b[1] + end) +else + table.sort(lines, function(a, b) + return a[1] < b[1] + end) +end + +for i, line in ipairs(lines) do + print(line[2]) +end