bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] How to define a function that can accept arbitrary number


From: Luuk
Subject: Re: [bug-gawk] How to define a function that can accept arbitrary number of arguments?
Date: Sat, 30 Apr 2016 10:59:04 +0200
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2



On 30-04-16 03:29, david kerns wrote:

On Fri, Apr 29, 2016 at 5:25 PM, Peng Yu <address@hidden> wrote:
Hi, I see that there are built-in functions in gawk that can accept
arbitrary number of arguments. How to define one with accept arbitrary
number of arguments by the users? Thanks.

       and(v1, v2 [, ...]) Return  the bitwise AND of the values
provided in the argument list.  There
                           must be at least two.

--
Regards,
Peng


nope, you'll have to loop on the list

echo 127 63 255 61 7 | awk '{r=$1;for(i=2;i<=NF;++i)r=and(r,$i);print r}'



or you could to something recursive,
in this example i add the values of the array elements:

function myadd(x,  s) {
        m = length(x);
        switch(m>0) {
                case 1:
                        s = s + x[m];
                        delete x[m];
                        return s+myadd(x);
                        break;
                default:
                        return s;
                        break;
                }
        }

BEGIN{
        ex = "127, 63, 255, 61, 7";
        split(ex, a, ",");
        print myadd(a);
        }


The output of this is: 513 

reply via email to

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