bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] How to test if a variable is specified for a function?


From: Peng Yu
Subject: Re: [bug-gawk] How to test if a variable is specified for a function?
Date: Wed, 28 Sep 2016 11:44:39 -0500

OK. I get it. Please ignore this message.

On Wednesday, September 28, 2016, Peng Yu <address@hidden> wrote:


On Wednesday, September 28, 2016, Davide Brini <address@hidden> wrote:
On Wed, 28 Sep 2016 08:46:13 -0500, Peng Yu <address@hidden> wrote:

> Hi, Some internal awk functions allows users to specify a variable
> number of arguments.
>
> asort(s [, d [, how] ])
>
> If I want to define a function that allows a variable number of
> arguments, I'd like to test what arguments are specified by users. Is
> there a way to do so in awk?

All user defined functions in awk accept variable number of arguments. If
you really want to check whether the user passed one of them, you can use
the same generic method used to check whether a variable is defined:


function foo(arg1, arg2,    optional) {

  if ((optional == "") && (optional == 0)) {

What if users pass an arg of 0 or ""?
 
    print "user did not pass optional arg"
  } else {
    print "user passed optional arg"
  }

}

BEGIN{ foo(1, 2, 3); foo(1, 2, ""); foo(1, 2, 456); foo(1, 2) }

The above outputs:

user passed optional arg
user passed optional arg
user passed optional arg
user did not pass optional arg


--
D.



--
Regards,
Peng


--
Regards,
Peng

reply via email to

[Prev in Thread] Current Thread [Next in Thread]