|
From: | Peng Yu |
Subject: | Re: [bug-gawk] Is there a way to assign to a hash via the -v option |
Date: | Mon, 7 Mar 2016 08:14:39 -0600 |
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
[Prev in Thread] | Current Thread | [Next in Thread] |