#!/usr/bin/env awk
#
# Keep track of the max value of a column 'c'.
# The result can be accessed with 'get_max(c)' or printed with 'print_max([c])'.
#
# Parameters:
#   - c : the column number to get the max of
#
# Dependencies:
#   - None
#
# See also:
#   - mean
#   - min
#
# Examples:
#   - tawk '{
#         max(pktsSnt)
#     } END {
#         print_max(pktsSnt)
#     }' file.txt
#   - tawk '{
#         max(pktsSnt)
#     } END {
#         print get_max(pktsSnt)
#     }' file.txt
#   - tawk '{
#         max(pktsSnt)
#         max(pktsRcvd)
#     } END {
#         print_max()
#     }' file.txt

@include "COL"
@include "hdr"

function max(c) {
    if (hdr()) next
    if (!(COL[c] in __MAX__) || $c > __MAX__[COL[c]]) {
        __MAX__[COL[c]] = $c
    }
}

function get_max(c) {
    return __MAX__[COL[c]]
}

function print_max(c,        _i) {
    if (!c) {
        for (_i in __MAX__) {
            print _i ": " __MAX__[_i]
        }
    } else {
        print get_max(c)
    }
}
