[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: |
arnold |
Subject: |
Re: [bug-gawk] How to test if a variable is specified for a function? |
Date: |
Wed, 28 Sep 2016 08:38:45 -0600 |
User-agent: |
Heirloom mailx 12.4 7/29/08 |
Excellent answer.
Thanks!
Arnold
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)) {
> 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.