diff --git a/interp/interp.go b/interp/interp.go index 7f5c8c3..594c111 100644 --- a/interp/interp.go +++ b/interp/interp.go @@ -906,7 +906,7 @@ func (p *interp) getFieldByName(name string) (value, error) { if p.fieldIndexes == nil { // Lazily create map of field names to indexes. if p.fieldNames == nil { - return null(), newError(`@ only supported if header parsing enabled; use -H or add "header" to INPUTMODE`) + return null(), newError(`no field names for @; use -H or add "header" to INPUTMODE, and use "getline" first if in BEGIN`) } p.fieldIndexes = make(map[string]int, len(p.fieldNames)) for i, n := range p.fieldNames { diff --git a/interp/interp_test.go b/interp/interp_test.go index 2083b90..5b7262c 100644 --- a/interp/interp_test.go +++ b/interp/interp_test.go @@ -1797,7 +1797,9 @@ var csvTests = []csvTest{ {`BEGIN { INPUTMODE="csv" } { print $1, $3 }`, "name,email,age\nBob,bob@smith.com,42\nJane,jane@brown.com,37", "name age\nBob 42\nJane 37\n", "", nil}, {`BEGIN { INPUTMODE="csv header" } { print @"age", @"name" }`, "name,email,age\nBob,bob@smith.com,42\nJane,jane@brown.com,37", "42 Bob\n37 Jane\n", "", nil}, {`BEGIN { INPUTMODE="csv header" } { x="name"; print @"age", @x }`, "name,age\nBob,42", "42 Bob\n", "", nil}, - {`BEGIN { INPUTMODE="csv" } { print @"age", @"name" }`, "name,email,age\nBob,bob@smith.com,42\nJane,jane@brown.com,37", "", `@ only supported if header parsing enabled; use -H or add "header" to INPUTMODE`, nil}, + {`BEGIN { INPUTMODE="csv" } { print @"age", @"name" }`, "name,email,age\nBob,bob@smith.com,42\nJane,jane@brown.com,37", "", `no field names for @; use -H or add "header" to INPUTMODE, and use "getline" first if in BEGIN`, nil}, + {`BEGIN { INPUTMODE="csv header"; print @"age", @"name" }`, "name,email,age\nBob,bob@smith.com,42\nJane,jane@brown.com,37", "", `no field names for @; use -H or add "header" to INPUTMODE, and use "getline" first if in BEGIN`, nil}, + {`BEGIN { INPUTMODE="csv header"; getline; print @"age", @"name" }`, "name,email,age\nBob,bob@smith.com,42\nJane,jane@brown.com,37", "42 Bob\n", "", nil}, {`BEGIN { INPUTMODE="tsv header" } { print $1, $3 }`, "name\temail\tage\nBob,Smith\tbob@smith.com\t42\nJane\tjane@brown.com\t37", "Bob,Smith 42\nJane 37\n", "", nil}, // OUTPUTMODE combinations