#! /usr/local/bin/apl --script ⍝ ******************************************************************** ⍝ $Id: $ ⍝ $desc: Library of useful apl functions $ ⍝ ******************************************************************** ⍝ Util ⍝ Copyright (C) 2016 Bill Daly ⍝ This program is free software: you can redistribute it and/or modify ⍝ it under the terms of the GNU General Public License as published by ⍝ the Free Software Foundation, either version 3 of the License, or ⍝ (at your option) any later version. ⍝ This program is distributed in the hope that it will be useful, ⍝ but WITHOUT ANY WARRANTY; without even the implied warranty of ⍝ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ⍝ GNU General Public License for more details. ⍝ You should have received a copy of the GNU General Public License ⍝ along with this program. If not, see . )copy 1 lex ∇rpt←bfld util∆breakon data;bv;b2;cols;bc;tl ⍝ Function builds a report of the data by inserting subtotals where ⍝ bfld changes cols←(,⊃∧⌿util∆numberp¨data)/⍳1↓⍴data tl←(1↓⍴data)⍴⊂' ' →(∧/bv←∧\bfld=bfld[1])/exit tl[cols]←+⌿bv⌿data[;cols] rpt←(bv⌿data),[1]tl,[1](b2/bfld) util∆breakon (b2←~bv)⌿data →0 exit: tl[cols]←+⌿data[;cols] rpt←data,[1]tl →0 ∇ ∇msg←util∆helpFns fn;src ⍝ Function to display help about a function src←⎕cr fn msg←(1,∧\'⍝'=1↓src[;1])⌿src ∇ ∇t←util∆numberp v ⍝ Fns returns true if its argument is a number. ⍎(1<≡v)/'t←0◊→0' t←''⍴0=1↑0⍴v ∇ ∇b←util∆numberis tst ⍝ Function test if a number can be obtained by executing a string b←(∧/tst∊'1234567890-¯.')∧∨/0 1=+/tst='.' b←b∧∧/~(1↓tst)∊'-¯' ∇ ∇a←t util∆over b;w;tr;br ⍝ fn to return an array (of rank 2) with t on the top and b on the bottom. t←(rt←¯2↑1 1,⍴t)⍴t b←(br←¯2↑1 1,⍴b)⍴b w←rt[2]⌈br[2] rt[2]←br[2]←w a←(rt↑t),[1]br↑b ∇ ∇new←ws util∆reduce∆whitespace txt;bv ⍝ Function to reduce whitespace to one space ⍎(2≠⎕nc'ws')/'ws←⎕av[10 11 14 33]' ⍝ Space, tab and line feed and return bv←txt∊ws bv←~bv∧(⍴bv)↑0,bv new←bv/txt new[(new∊ws)/⍳⍴new]←' ' ∇ ∇New←util∆trim old;b ⍝ Function to strip off leading and trailing spaces. New←(+/∧\old=' ')⌽old b←⌊/+/∧\⌽New=' ' →(V,M,E)[3⌊⍴⍴old] ⍝ Vector V: New←New[⍳-b-⍴New] →0 ⍝ Matrix M: New←New[;⍳-b-1↓⍴New] →0 ⍝ Error -- rank of old is too high E: ⎕es 'Rank of array is too high' ∇ ∇o←k util∆sub d ⍝ Function to calculate subtotals for each break point in larg o←+\[1]d ⍝ Test for rank of data ⎕es (~(⍴⍴d) ∊ 1 2)/'RANK ERROR' →(V,A)[⍴⍴d] ⍝ Vectors V:o←o[k]-¯1↓0,o[k] →0 ⍝ Arrays (of rank 2) A: o←o[k;]-0,[1] o[¯1↓k;] →0 ∇ ∇n←util∆import∆numbers s;bv ⍝ Function to turn a column of figures (ie characters) into numbers ⍎(2=≡s)/'s←⊃s' bv←~∧/s=' ' s[(s='(')/⍳⍴s←,' ',s]←'¯' n←bv\⍎(~s∊'),')/s ∇ ∇string←delim util∆join array ⍝ Function returns a character string with delim delimiting the items ⍝ in array. string←1↓∊,delim,(⌽1,⍴array)⍴array ∇ ∇v←delim util∆split string;b;c ⍝ Function splits a character string into a nested array of strings ⍝ using delim as the delimiter. →(1≠⍴delim←,delim)/many →(∧/b←string ≠ delim)/last →exit many: →(∧/~b←string∊delim)/last string←(c←~b∧1⌽b)/string b←c/~b →exit exit: v←(⊂b/string),delim util∆split 1↓(~b←∧\b)/string →0 last: v←⊂string ∇ ∇ix← list util∆stringSearch item;rl;ri;l ⍝ Utility to search a character list for an item. →(1=≡list)/arr list←⊃list arr: ⎕es(2≠⍴rl←⍴list)/'RANK ERROR' ri←⍴item←,item l←rl[2]⌈ri ix←(((rl[1],l)↑list)∧.=l↑,item)⌿⍳rl[1] ∇ ∇ix←txt util∆search word;⎕io;old∆io;ixx;bv ⍝ Function to search for larg in rarg old∆io←⎕io ⎕io←0 ixx←⍳⍴txt←,txt bv←(txt=1↑word←,word)∧ixx≤(⍴txt)-⍴word ix←bv/ixx ix←old∆io+(txt[ix∘.+⍳⍴word]∧.=word)/ix ∇ ∇new←txt util∆replace args;ix ⍝ Function to search for and replace an item in rarg. Larg is a two ⍝ element vector where Larg[1] is the text to search for, Larg[2] is ⍝ the replacement text. ix← txt util∆search ⊃args[1] new←((¯1+ix)↑txt),(,⊃args[2]),(¯1+(ix←''⍴ix)+⍴,⊃args[1])↓txt ∇ ∇resp←util∆prompt prompt;test;sink;ans ⍝ Subroutine to prompt for user input. sink←⍞←prompt ans←⍞ test←(∧\ans≠' ')/ans →(util∆numberp test←util∆∆keys ans)/kw resp←ans →0 kw: resp←test →0 ∇ ∇t←n util∆execTime c;ts;lb;i ⍝ Function returns the number of milliseconds a command took. larg ⍝ is the number of to execute command. If larg is missing we ⍝ execute once. →(2=⎕nc 'n')/many ts←⎕ts ⍎c →ed many: lb←(n⍴st),ed i←0 ts←⎕ts st: ⍎c →lb[i←i+1] ed: t←⎕ts t←(60 1000⊥t[6 7])-60 1000⊥ts[6 7] →0 ∇ ∇o←util∆∆keys i;k ⍝ Function to return a keyword number if i is a keyword. k←((4 4 ⍴ 'quittop doneback')∧.=4↑o←,i)/⍳4 →((4<⍴i)∨0=⍴k)/0 o←k-⎕io →0 ∇ ∇today←util∆today;ts ⍝ Function to return today's date as a string ts←⎕ts[2 3 1] today←('05'⍕ts[1]),'/',('05'⍕ts[2]),'/','5000'⍕ts[3] ∇ ∇txt←util∆lower m;ix ⍝ Function to convert text to all lower case. m←⎕ucs m←,m ix←((m≥65)∧m≤90)/⍳⍴m m[ix]←m[ix]+32 txt←⎕ucs m ∇ ∇txt←util∆upper m;ix ⍝ Function to convert text to all lower case. m←⎕ucs m←,m ix←((m≥97)∧m≤122)/⍳⍴m m[ix]←m[ix]-32 txt←⎕ucs m ∇ ∇Z←util⍙metadata Z←0 2⍴⍬ Z←Z⍪'Author' 'Bill Daly' Z←Z⍪'BugEmail' 'address@hidden' Z←Z⍪'Documentation' 'lex.txt' Z←Z⍪'Download' 'https://sourceforge.net/p/apl-library/code/ci/master/tree/util.apl' Z←Z⍪'License' 'GPL v3.0' Z←Z⍪'Portability' 'L1' Z←Z⍪'Provides' '' Z←Z⍪'Requires' '' Z←Z⍪'Version' '0 0 5' ∇