Browse Source

comment awk_total; simplify insertion sort and print

typed_gg
user030276 4 months ago
parent
commit
4fa4c95a2e
  1. 86
      awk_total

86
awk_total

@ -1,13 +1,12 @@ @@ -1,13 +1,12 @@
#!/usr/bin/awk
# total outputs from awk_extract
# provide summary and sorting
# provide summary and sorting for awk_extract
BEGIN {
RS="\n\n+" # set multiline records
gc=0 # set game count
# awk_extract generates 42 fields
# awk_total recieves 2 additional prefacing fields
# awk_total receives 2 additional prefacing fields
tf=44 # total number of fields
i=15 # initial field player 1
j=30 # initial field player 2
@ -15,14 +14,14 @@ BEGIN { @@ -15,14 +14,14 @@ BEGIN {
# ignore incomplete records
NF==tf {
# the 'complete' array
# the complete array
com[NR]=$0"\n"
gc++
# remove %
{ gsub(/%/,"") }
# the 'points' arrays; each field generated by awk_extract
# the points arrays; each field generated by awk_extract
for (k=0; k<(j-i); k++)
pnt[NR,k]=$(i+k)+$(j+k)
@ -30,18 +29,18 @@ NF==tf { @@ -30,18 +29,18 @@ NF==tf {
abs=$i-$j
pnt[NR,1]=abs < 0 ? -abs: abs
# creating 'victory margin' array
# creating victory margin array
if (pnt[NR,1]>3) vm[4]++
else vm[pnt[NR,1]]++
# the 'player name' array
# the player name array
pn[$(i+1)]
pn[$(j+1)]
for (k in pn) {
# if player matches field
# add to 'player point' array
# increment 'player game count' array
# maybe increment 'player win' array
# add to player point array
# increment player game count array
# maybe increment player win array
if (k==$(i+1)) {
pp[k]+=$i
pgc[k]++
@ -53,7 +52,7 @@ NF==tf { @@ -53,7 +52,7 @@ NF==tf {
}
}
} END {
# if we have games
# if we have a game
if (gc) {
# for all players
for (i in pn) {
@ -79,7 +78,7 @@ NF==tf { @@ -79,7 +78,7 @@ NF==tf {
for (i=0;i<5;i++)
!vm[i] && vm[i]=0
# column headers
# victory margin column headers
printf "%-*s",9,""
printf "%*s",6,"zero"
printf "%*s",6,"one"
@ -97,42 +96,55 @@ NF==tf { @@ -97,42 +96,55 @@ NF==tf {
printf "\n"
# sort and print according to what?
# each field generated by awk_extract has been summed into a points array
# send a field to the insertion sort function
# always print the complete array
# if sorting by points differential, forward print
# else if some other sort, reverse print
# else forward print
if (s==1) {
isort(pnt, s, com, NR)
fprint(com, NR)
isort(s)
fprint()
} else if (s) {
s ~ /pt/ && s=0 # numeric 0 fails boolean context
isort(pnt, s, com, NR)
rprint(com, NR)
} else fprint(com, NR)
# points total is points array 0
s ~ /pt/ && s=0
isort(s)
rprint()
} else fprint()
}
}
#reverse print
function rprint (A,n, i) {
for(i=n;i>=1;i--)
if (A[i])
print A[i]
function rprint (i) {
for(i=NR;i>=0;i--)
if (com[i])
print com[i]
}
#forward print
function fprint (A,n, i) {
for (i=0;i<=n;i++)
if (A[i])
print A[i]
function fprint (i) {
for (i=0;i<=NR;i++)
if (com[i])
print com[i]
}
# insertion sort of one 'points' array reorders the 'complete' array
# reorder by any field generated by awk_extract
function isort(A, k, B, n, i, j, hold0, hold1) {
for (i=2;i<=n;i++) {
hold0 = A[j=i,k]
hold1 = B[j=i]
while ( A[j-1,k] > hold0 ) {
# isort of one points array reorders the complete array
function isort(s, i, j, hold0, hold1) {
# for each record
for (i=0;i<=NR;i++) {
# hold both arrays at current record
hold0 = pnt[j=i,s]
hold1 = com[j=i]
# while point array at previous record is greater than its hold
while ( pnt[j-1,s] > hold0 ) {
# set next record of both arrays
j--
A[j+1,k]=A[j,k]
B[j+1]=B[j]
pnt[j+1,s]=pnt[j,s]
com[j+1]=com[j]
}
A[j,k] = hold0
B[j] = hold1
# reset holds
pnt[j,s] = hold0
com[j] = hold1
}
}

Loading…
Cancel
Save