bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] SYMTAB strangeness


From: Aharon Robbins
Subject: Re: [bug-gawk] SYMTAB strangeness
Date: Sat, 15 Dec 2012 22:32:49 +0200
User-agent: Heirloom mailx 12.5 6/20/10

Hi Assaf.

> (Continuing a previous discussion about SYMTAB:
> http://lists.gnu.org/archive/html/bug-gawk/2012-11/msg00023.html)
> 
> My goal is to add a little BEGIN code, that will read the first line
> from a file, create variables for each header/column name.

You can't do that. See below.

> With the latest git revision, the following examples cause "internal
> error".
>
> This works with latest revision - SYMTAB names are explicit:
>
>     $ printf "Name Age\nJohn 30\nJane 40\n" | \
>         gawk 'BEGIN { SYMTAB["Name"]=1 ; SYMTAB["Age"]=2 } { print $Age }'
>     Age
>     30
>     40
>
> This fails - SYMTAB names are read from a variable.
> (but conceptually, IMHO, this seems equivalent to the previous example)
>
>     $ printf "Name Age\nJohn 30\nJane 40\n" | \
>         gawk 'BEGIN { getline ; for (i=1;i<=NF;++i) SYMTAB[$i]=i } { print 
> $Age }'
>     gawk: cmd. line:1: (FILENAME=- FNR=2) fatal: internal error line 175, 
> file: interpret.h

There are a number of peculiarities in the way SYMTAB works and its
interactions with the rest of the code. This is a bug. I'll work on it.

> Similarly strange (probably related):
>
> "test" is printed as a valid variable:
>
>     $ echo | gawk -d/dev/stdout '{ SYMTAB["test"]=1 } END { print $test }' | 
> grep test
>     test: 1

That's because it's used as a variable in the program text.

> "test" is *not* printed as a valid variable
>
>     $ echo | gawk -d/dev/stdout '{ SYMTAB["test"]=1 }' | grep test

Because it's not used as a variable. In this case, SYMTAB acts like
a regular associative array that stores subscripts and values.

The only way to create variables is to use the identifiers in the text
of the program or via the command line. (Probably another interaction
there waiting to trip us up.)

I am starting to think that this may be a design mistake and that
it should not be allowed to add values to SYMTAB that aren't variables
referenced in the text of the program.

> This generates internal error:
>
>     $ echo test | gawk -d/dev/stdout '{ SYMTAB[$1]=1 } END { print $test }' | 
> grep test
>     gawk: (FILENAME=- FNR=1) fatal error: internal error
>     test

Yet another bug of some kind.

Thanks for the reports. I'll work on these.

Arnold



reply via email to

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