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: Aharon Robbins
Subject: Re: [bug-gawk] built-in variables in extensions
Date: Tue, 18 Dec 2012 18:42:14 +0200
User-agent: Heirloom mailx 12.5 6/20/10

Hi Andy.

> [...] 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.

This is like totally trivial to write as an awk function, and the performance
hit is next to nil:

        # untested
        function spliti(string, array, sep,             i, count, temp)
        {
                delete array
                if (sep == "")
                        sep = FS

                count = split(string, temp, sep)
                for (i in temp)
                        array[temp[i]] = i

                return count
        }

There really is no reason to write it in C...

My two cents.

Arnold



reply via email to

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