[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Is there a way to assign to a hash via the -v option
From: |
Aharon Robbins |
Subject: |
Re: [bug-gawk] Is there a way to assign to a hash via the -v option |
Date: |
Mon, 07 Mar 2016 05:52:53 +0200 |
User-agent: |
Heirloom mailx 12.5 6/20/10 |
Hi.
As noted, there is no way to directly assign to an array element using -v.
This isn't going to change, since the general case would require being
able to parse any arbitrary expression as the subscript.
David's suggestion is one option. Another is simply to mix the -e and -f
command line options and do the assignment in a BEGIN rule suppplied
on the command line. Shell quoting tricks can be used if you need to
assign a dynamically computed value.
Arnold
> Date: Fri, 4 Mar 2016 15:22:33 -0700
> From: david kerns <address@hidden>
> To: Peng Yu <address@hidden>
> Cc: address@hidden
> Subject: Re: [bug-gawk] Is there a way to assign to a hash via the -v option
>
> how about this:
>
> awk -v l="10,20" 'BEGIN{split(l,x,","); print x[1]; print x[2]}'
>
> On Fri, Mar 4, 2016 at 1:56 PM, Peng Yu <address@hidden> wrote:
>
> > Hi, I want to assign to a hash via the -v option. But the following
> > does work. Is there anyway I can do it with -v?
> >
> > $ awk -v "x[1]=10" -v "x[2]=20" 'BEGIN{print x[1]; print x[2]}'
> > awk: fatal: `x[1]' is not a legal variable name
> >
> > --
> > Regards,
> > Peng