• Once upon a time... (Was: printing words without newlines?)

    From Kenny McCormack@21:1/5 to janis_papanagnou+ng@hotmail.com on Thu May 16 14:15:59 2024
    XPost: alt.comp.lang.awk

    In article <v2538p$1jmvm$1@dont-email.me>,
    Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
    ...
    A function definition called once from the BEGIN section isn't
    called "once per input line".

    Especially since it is commented out, so it executes exactly zero times.

    Actually setting ORS (or any other similar variable) inside a function definition is not such a bad idea, in terms of modularity.

    --
    To all the people worried about how bad it would look to have a public trial of a
    former president (and all the usual verbiage that we heard in 1974), I say this to DJT:
    Just plead guilty, take your medicine, do your time, just fade away.
    For the good of the country. Do the right thing.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to Kenny McCormack on Thu May 16 15:17:42 2024
    XPost: alt.comp.lang.awk

    In article <v254ev$125p2$1@news.xmission.com>,
    Kenny McCormack <gazelle@shell.xmission.com> wrote:
    In article <v2538p$1jmvm$1@dont-email.me>,
    Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
    ...
    A function definition called once from the BEGIN section isn't
    called "once per input line".

    Especially since it is commented out, so it executes exactly zero times.

    Actually setting ORS (or any other similar variable) inside a function >definition is not such a bad idea, in terms of modularity.

    In fact, I'd like to expand on that. It is commonly held that a
    well-written function that changes the values of "special variables" should save and restore them. I.e.:

    function foo(arg1, arg2, ...) {
    oldORS = ORS
    ORS = new value
    ...
    ORS = oldORS
    }

    But in fact, in practice, this can get tricky - due to vagaries of the AWK language. What would really be nice is if you could declare special
    variables in the parameter list - which would give them the "local
    variable" treatment. I.e.:

    function foo(arg1, arg2, ..., ORS) {
    ORS = new value
    ...
    }

    Now, ORS would be magically restored to its previous value w/o the function having to deal with it (**). Unfortunately, neither GAWK nor TAWK allows this. GAWK gives an error message saying you can't use special variables in arg lists. TAWK just silently ignores the attempt.

    What would be even better is if this happened magically w/o needing to do
    the above parameter trick. An argument can be made that changes to special variables should, by default, be local to functions. Now, as it happens,
    this would break one of my functions - which I call "setsort", which sets PROCINFO["sorted_in"] for me. Basically, I can never remember the special names of the internal sorting functions (e.g., @ind_whatever), so I wrote a function setsort() and can now just do: setsort(1) to get the most commonly used sorting functionality. I find it easier to remember the numbers than
    to remember the exact spelling of those names.

    This, in turn, could be fixed if there was a "global" statement that would
    make a selected variable global rather than local (*). This is, in part, inspired by Tcl syntax, where everything is local by default and you have
    to explicitly use "global var" to make "var" global. I've often thought
    that, if it could be done all over again, AWK might be better if it had followed the Tcl model for function variables. Of course, it can't be
    changed now.

    (*) So, my setsort() function, I would write: global PROCINFO
    and that would make changes to PROCINFO visible to the caller.

    (**) Or, you could even pass a value for ORS in as part of the function call.

    --
    The randomly chosen signature file that would have appeared here is more than 4 lines long. As such, it violates one or more Usenet RFCs. In order to remain in compliance with said RFCs, the actual sig can be found at the following URL:
    http://user.xmission.com/~gazelle/Sigs/PennJillette

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)