#=head1 Awk library #=image right URL/share/etc/img/ben_yegnuda_small.png #=head2 Copyright #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, version 2. #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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #=head2 Usage # gawk -f lib.awk -f yourOtherFiles #=head1 Source code #=head2 Useful Globals BEGIN { Stderror="/dev/stderr" Q = "\""; _ = SUBSEP; Inf = 1.99999*(2**(127)); NegInf = -1*2**(126); White="^[ \t\n\r]*$"; Pi= 1068966896 / 340262731; # good to 17 decimal places Ee="2.71828182845905"; Number="^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+)([eE][+-]?[0-9]+)?$"; } #=head2 Printing magic function sayln(str) {say(str "\n")} function say(str) {printf str >Stderror; fflush(Stderror)} function barph(str) {print "#WARNING: " str>Stderror} function die(str) {print "#ERROR: " str>Stderror; exit 0} #=head2 Array Magic #These array store the size of the array in position C. function top(a) {return a[a[0]]} function dim(a) {return a[0]} function empty(a) {return a[0]==0} function push(a,x, i) {i=++a[0]; a[i]=x; return i} function pushes(ain,aout, i) {for(i in ain) push(aout,ain[i])} function push2(a,x,y, i) {i=++a[x,0]; a[x,i]=y; return i} function pop(a, x,i) { i=a[0]--; if (!i) {return ""} else {x=a[i]; delete a[i]; return x}} #Arrays to strings. function printa(a, i,sep) { printf "{"; for ( i in a ) { printf sep i "=" a[i]; sep=", "; } print "}"; } function printaln(a, i) {for(i in a) print i " " a[i]} function dump(a, b4,i) { for(i=1;i<=a[0];i++) printf b4 a[i] } function saya(s,a) {print s; print a2s(a)} function a2s(a, n,pre,i,j,str) { for(i in a) { j=i; gsub(SUBSEP,",",j); str= str pre "[" j "]=[" a[i] "]\n"; } return str; } #Set difference. A3=A1-A2 function minus(a1,a2,a3) { for(i in a1) if ( ! (i in a2)) {a3[i]=a1[i]} } #Copying lists. function copy(old,new, i,j) { delete new; for(i in old) {j++; new[i]=old[i]}; return j } #Binary chop on key-value pairs. #Return the key in "a", of length "most" #whose value is closest to "x". function binchop(a,x,most, left,middle,right) { left=1; right=most; while (left < right) { middle=int((left+right)/2); if (x> a[middle]) {left=middle+1} else {right=middle}; }; if (x=a[left]) {return left} else {return 0}; } #=head2 String magic function number(x) { return x ~ Number } function symbol(x) { return ! number(x) } function blank(x) { return x~/^[ \t]*$/ } function trimLeft(x) { sub(/^[ \t]*/,"",x); return x} function trimRight(x) { sub(/[ \t]*$/,"",x); return x} function trim(x, y) { return trimRight(trimLeft(x))} function str2keys(str,keys,sep, n,i,tmp) { n=split(str,tmp,sep); for(i in tmp) keys[tmp[i]]; keys[0]=n; return n; } function s2a(str,a,sep, n,i,tmp) { n=split(str,tmp,sep); for(i=1;i<=n;i=i+2) a[tmp[i]]=tmp[i+1]; return n; } function s2i(str,a,sep, n,i,tmp) { n=split(str,tmp,sep); for(i=1;i<=n;i++) a[i]=tmp[i]; return n; } function s2n(str,a,sep, n) { n=s2i(str,a,sep); a[0]=n; return n; } function str(n,c, x) { while ((n--) > 0) x= x c; return x; } #=head2 Match magic function lhs(s) {return substr(s, 1, RSTART-1)} function mid(s,x,y) {return substr(s, RSTART + x, RLENGTH - x - y)} function rhs(s) {return substr(s, RSTART+RLENGTH)} #=head2 Number magic function abs(n) {if (n < 0) {return -1*n} else {return n}} function odd(x) {return x % 2} function even(x) {return ! odd(x)} function most(x,y) { if (x>y) {return x} else {return y}} function least(x,y) { if (x, print it to the end of some I. function slurp(infile,outfile, line) { while ( (getline line < file) >=0 ) print line>>outfile; close(infile); } #Check if some file exits. function exists(file, dummy, ret) { ret=0;print 1; if ( (getline dummy < file) >=0 ) {ret = 1; close(file)}; return ret; } #=head2 Symbol table magic #The old symbol table trick. If the counter on this column is one... function novel(x,val,counter) {return ++counterl[x,val]== 1} #... then this is the first time we have #seen this symbol. So we have something new to remember. function remember(x,val,symbols) {symbols[x,++symbols[x,0]]=val} #=head2 Reading Tabular Data #Suppose we wanted to read in the following tables into a three #dimenstional table #I from the file I. # =effortMultipliers2000,xl, vl, l, n, h, vh, xh # rely, , 0.82, 0.92, 1.00, 1.10, 1.26, # data, , , 0.90, 1.00, 1.14, 1.28, # cplx, , 0.73, 0.87, 1.00, 1.17, 1.34, 1.74 # ruse, , , 0.95, 1.00, 1.07, 1.15, 1.24 # =scaleFactors2000,xl, vl, l, n, h, vh, xh # prec, , 6.20, 4.96, 3.72, 2.48, 1.24, # flex, , 5.07, 4.05, 3.04, 2.03, 1.01, # resl, , 7.07, 5.65, 4.24, 2.83, 1.41, # team, , 5.48, 4.38, 3.29, 2.19, 1.10, # pmat, , 7.80, 6.24, 4.68, 3.12, 1.56, # After calling # I # then I 0) { n=split(line,f,/,/); lines++ for(i=1;i<=n;i++) f[i]=trim(f[i]); if (match(f[1],/^=/)) { tab=rhs(f[1]); for(i=1;i