bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] built-in variables in extensions


From: Andrew J. Schorr
Subject: Re: [bug-gawk] built-in variables in extensions
Date: Mon, 17 Dec 2012 15:58:47 -0500
User-agent: Mutt/1.5.21 (2010-09-15)

Hi Assaf,

On Mon, Dec 17, 2012 at 03:45:23PM -0500, Assaf Gordon wrote:
> Continuing my quest for header-line support in gawk...
> Perhaps it's too complicated without internal code changes - in which case 
> I'll drop it. WDYT?

I'm curious -- what are you trying to achieve?  Many of my scripts
start like this:

# parse header record: map column names to field numbers
NR == 1 {
   for (i = 1; i <= NF; i++)
      m[$i] = i
   next
}

I can then access fields by column title, e.g. $m["name"]

I have often thought that it would be nice to have an inverse split function,
perhaps called "spliti", that would allow me to say instead:

NR == 1 {
   spliti($0, m)
   next
}

I think it should be possible to write an extension function that does this,
although it would be much easier to implement this as an internal gawk function
that has access to gawk's internal regexp splitting capability.

One very simple approach is to have an include file called parse_header.awk
that includes the above logic.  Then just say gawk -i parse_header (or put
@include "parse_header") in your code.

Based on your previous code, I guess you want to be able to use $name or $age.
I'd argue that this is a dangerous approach, since you risk conflicts with
variable names used elsewhere in your code...

Regards,
Andy



reply via email to

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