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

Liste des GroupesRevenir à cl awk 
Sujet : Re: Once upon a time... (Was: printing words without newlines?)
De : gazelle (at) *nospam* shell.xmission.com (Kenny McCormack)
Groupes : alt.comp.lang.awk comp.lang.awk
Date : 16. May 2024, 16:17:42
Autres entêtes
Organisation : The official candy of the new Millennium
Message-ID : <v2582m$125p2$2@news.xmission.com>
References : 1 2 3 4
User-Agent : trn 4.0-test77 (Sep 1, 2010)
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

Date Sujet#  Auteur
12 May 24 * printing words without newlines?19David Chmelik
12 May 24 +* Re: printing words without newlines?5Bruce Horrocks
12 May 24 i+- Re: printing words without newlines?1Bruce Horrocks
12 May 24 i`* Re: printing words without newlines?3Kenny McCormack
13 May 24 i +- Re: printing words without newlines?1David Chmelik
13 May 24 i `- Re: printing words without newlines?1Kaz Kylheku
13 May 24 +* Re: printing words without newlines?3Kenny McCormack
13 May 24 i`* Re: printing words without newlines?2Kenny McCormack
15 Jul 24 i `- Resurrecting an old thread (Was: printing words without newlines?)1Kenny McCormack
13 May 24 +- Re: printing words without newlines?1Janis Papanagnou
13 May 24 +* Re: printing words without newlines?4Kaz Kylheku
13 May 24 i`* Re: printing words without newlines?3Kenny McCormack
14 May 24 i `* Re: printing words without newlines?2Kaz Kylheku
14 May 24 i  `- Array indices are small integers? (Was: printing words without newlines?)1Kenny McCormack
16 May 24 `* Re: printing words without newlines?5Ed Morton
16 May 24  `* Re: printing words without newlines?4Janis Papanagnou
16 May 24   +* Once upon a time... (Was: printing words without newlines?)2Kenny McCormack
16 May 24   i`- Re: Once upon a time... (Was: printing words without newlines?)1Kenny McCormack
17 May 24   `- Re: printing words without newlines?1Ed Morton

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal