Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

The biggest difference between time and space is that you can't reuse time. -- Merrick Furst


devel / comp.lang.awk / Feature request (Attn: GAWK developers): Arbitrary args in GAWK?

SubjectAuthor
o Feature request (Attn: GAWK developers): Arbitrary args in GAWK?Kenny McCormack

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

<ss6jk6$3alh8$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=995&group=comp.lang.awk#995

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.awk
Subject: Feature request (Attn: GAWK developers): Arbitrary args in GAWK?
Date: Tue, 18 Jan 2022 14:42:15 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <ss6jk6$3alh8$1@news.xmission.com>
Injection-Date: Tue, 18 Jan 2022 14:42:15 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="3495464"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Tue, 18 Jan 2022 14:42 UTC

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.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor