• Feature request (Attn: GAWK developers): Arbitrary args in GAWK?

    From Kenny McCormack@21:1/5 to All on Tue Jan 18 14:42:15 2022
    One feature, found in TAWK, that I'd really like to see in GAWK is the
    ability to define functions (written in AWK) that take an arbitrary number
    of args. Note that this is possible in an extension library function
    (written in C), but not (as of this writing) in functions written in AWK.
    More about this below.

    The canonical example of this is writing a "max" function - that will
    calculate the maximum of an arbitrary list of numbers passed. The point is that you want it to work with any number of parameters passed. In TAWK, we
    can write:

    --- Cut Here ---
    function max() {
    local ac = argcount(),mx = argval(1), i, tmp

    if (ac == 0) return "Invalid call - no args at all!"
    for (i=2; i<=ac; i++)
    if ((tmp = argval(i)) > mx)
    mx = tmp
    return mx
    }
    BEGIN {
    print max()
    print max(1)
    print max(-1,-5)
    print max(-1,5,-5)
    print max(10,2)
    }
    --- Cut Here ---

    As I said earlier, it is possible to do this in an extension library
    function. In an extension library function, the API gives you the two
    things you need:
    1) A count of the number of args passed.
    2) A way to access those args by number (position) - rather than by name.

    These, of course, correspond directly to TAWK's argcount() and argval() functions. Given that they are present in the extension library API, it
    seems clear to me that it shouldn't be at all difficult to expose them to
    the normal AWK programming environment.

    All in all, this is a feature that I'd like to see implemented.

    Note: Not really interested