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.awkDate : 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 4lines long. As such, it violates one or more Usenet RFCs. In order to remainin compliance with said RFCs, the actual sig can be found at the following URL: http://user.xmission.com/~gazelle/Sigs/PennJillette