ezrosent/frawk

Checking a csv header unexpectedly adds it to FI

andrewchambers opened this issue · 1 comments

Program

{
	if (FI["non-existent-header"]) {
		print "nothing..."
	}
	for (k in FI) {
		print "k =", k, 
	}
}

Invocation:

$echo -e 'a,b,c\n1,2,3' | frawk -i csv -H -f program.awk
k = c
k = non-existent-header
k = a
k = b

Thanks for the report!

This may be unexpected, but we take this behavior directly from awk: array subscripts like FI[x] will insert x into the map if it isn't there in the process of looking things up. (See pages 51-52 of the AWK book).

To test if something is in a map/array you can use the if ("non-existent-header" in FI) syntax:

{
	if ("non-existent-header" in FI) {
		print "nothing..."
	}
	for (k in FI) {
		print "k =", k;
	}
}

Should do what you want.