Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

user functions with variable number of parameters

93 views
Skip to first unread message

roby

unread,
Apr 20, 2009, 3:34:32 AM4/20/09
to
Hi all.

Is it possible with GAWK 3.1.6 to create a function witha varaible
number of parametrs?

Thanks all.

Roby

Ted Davis

unread,
Apr 20, 2009, 8:45:29 AM4/20/09
to
On Mon, 20 Apr 2009 00:34:32 -0700, roby wrote:

> Hi all.
>
> Is it possible with GAWK 3.1.6 to create a function witha varaible number
> of parametrs?
>

It would help to know what you are trying to do.

AWK doesn't have functions with an unknowable number of parameters, but
all functions are happy if you don't use all the ones you declare (unless
they can't deal with values of zero or null for the unused ones).

--
T.E.D. (tda...@mst.edu)

Tim Menzies

unread,
Apr 20, 2009, 10:23:33 AM4/20/09
to

one way to do is to define a function with more arguments than you
need.

then you print each arg till you hit a nil argument, then stop

for example: http://unbox.org/wisp/trunk/requirez/lib/string/prints.awk

this is used to print long strings without needed to fuss with "\"
characters, see below. note that the "prints" function knows to

a) outout a new line if it sees a string with one character
b) indent with one tab any other line starting with a tab.

function usage() { about(Who,What,When,Why);

prints("Usage: "What" [FLAGS] FILE"," ",
"FILE is columns of data."," ",
" -b NUM width of bars; default=["BarColumnWidth"]",
" -c show copyright",
" -d CHAR field seperator",
" -h show help",
" -k NUM width of 'key' column;default=["KeyColumnWidth"]",
" -m CHAR character to draw one histogram dot; default=["Mark"]",
" -n no sorting of keys",
" -s provides mean and standard deviation of each bar",
" -t NUM target column; default=["Collect"]",
" -v NUM width of 'value' column; default=["ValueColumnWidth"]",
" -w NUM width of each data bin; default=["Round"]");
}

enjoy!
:-)

t

p.s.
this could be a generic tool with the indirect function extension
offered by Robbins in the forthcoming Gawk 3.2 (the function to be
called on the first non-nil arguments could be passed in as arg1)

Janis Papanagnou

unread,
Apr 20, 2009, 2:13:22 PM4/20/09
to
roby wrote:
> Hi all.
>
> Is it possible with GAWK 3.1.6 to create a function witha varaible
> number of parametrs?

Depending on the application context I'd probably use one of the three
ways outlined here...


function f(args)
{
for (i=1; i in args; i++)
print "==" args[i] "=="

}

function g(arg)
{
n = split(arg,arr,"|")
for (i=1; i<=n; i++)
print "==" arr[i] "=="
}

BEGIN {
a[++i] = "Hello" ; a[++i] = "world" ; f(a)

b = "Hello|my|good|old|friend" ; g(b)

c = "All|along|the|watchtower" ; split(c,l,"|") ; f(l)
}


Janis

>
> Thanks all.
>
> Roby
>

roby

unread,
Apr 21, 2009, 3:28:26 AM4/21/09
to
On Apr 20, 8:13 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:

Beautiful, elegant and complete.

Many thanks.
-re-

roby

unread,
Apr 21, 2009, 3:30:53 AM4/21/09
to

Very interesting solution.

Where can I found GAWK 3.2?

Many thanks
-re-

Steffen Schuler

unread,
Apr 21, 2009, 3:37:52 PM4/21/09
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2009-04-21, roby <ellero...@katamail.com> wrote:
> [...]


> Where can I found GAWK 3.2?

> [...]

http://tinyurl.com/future-gawk-3-2

- --
Steffen Schuler (goedel) <schuler...@gmail.com>
Key ID: 0x42C5D853 / Key-server: pgp.mit.edu
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAknuIG8ACgkQ+/TWb0LF2FPi5ACfeASbjsgmvMXSLgmD+I7NeMXP
P7MAn3YGD4Q8vO18y2Oe+j+Ts0vyJY3e
=1aJE
-----END PGP SIGNATURE-----

GLT

unread,
Apr 23, 2009, 1:33:54 PM4/23/09
to
On Apr 21, 3:37 pm, Steffen Schuler <schuler.stef...@gmail.com> wrote:
>
>  http://tinyurl.com/future-gawk-3-2
>

That doesn't look like the kind of tinyurl link I'm used to seeing;
but maybe they've changed how it works?

When I try to use it, though, the tinyurl server never responds. Thus
I wonder if the link is really correct?

Janis Papanagnou

unread,
Apr 23, 2009, 1:41:35 PM4/23/09
to
GLT wrote:
> On Apr 21, 3:37 pm, Steffen Schuler <schuler.stef...@gmail.com> wrote:
>
>> http://tinyurl.com/future-gawk-3-2
>>
>
>
> That doesn't look like the kind of tinyurl link I'm used to seeing;
> but maybe they've changed how it works?

I also wondered about that readable link...

>
> When I try to use it, though, the tinyurl server never responds. Thus
> I wonder if the link is really correct?

...but the server responded. The referenced address is

http://cvs.savannah.gnu.org/viewvc/gawk-devel/?root=gawk


Janis

roby

unread,
Apr 24, 2009, 1:20:28 PM4/24/09
to
On Apr 23, 7:41 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:

Where can I found the EXE version for Windows XP Professional OS?

Otherwise how can use the source under Cygwin?

Many thanks
Roby

Kenny McCormack

unread,
Apr 24, 2009, 3:40:15 PM4/24/09
to
In article <145a6472-52b5-449d...@t21g2000yqi.googlegroups.com>,
roby <ellero...@katamail.com> wrote:
...

>Where can I found the EXE version for Windows XP Professional OS?
>
>Otherwise how can use the source under Cygwin?

It should be as simple as the usual: ./configure && make

It's been a while since I've done it, but it was just that simple.

Have you tried this yet?

0 new messages