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

    From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.lang.awk on Tue Jan 18 14:42:15 2022
    From Newsgroup: comp.lang.awk

    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 in work-arounds or comments to the effect of
    "This is not the right forum" or whatever. Just hoping that one of the
    devs sees this post - and gets around to implementing it before I do (heh heh).

    Note also that a common work-around is to put the things into an array and
    then pass the array to the function. This is workable sometimes, but is
    often inconvenient.
    A Catholic woman tells her husband to buy Viagra.

    A Jewish woman tells her husband to buy Pfizer.
    --- Synchronet 3.19b-Linux NewsLink 1.113