Observe:
$ gawk4 '{ x[$1] = $0 } END { if (!length(x)) print "Nothing in the array";for (i in x) print i,x[i] }'
Nothing in the array
gawk4: cmd. line:1: fatal: attempt to use scalar `x' as an array
$
This happens because I hit ^D (eof) as the first input to this program.
I can "fix" this issue by either:
1) Entering at least one valid line of input before hitting EOF.
or
2) Reversing the order of the two clauses in the END section.
Now, of course, it is clear what is going on here - and why the "fixes"
work. But what surprises me is that it (the error) happens at all. My understanding had been that the issue (i.e., dark corner in the GAWK language) of whether something is an array or a scalar is resolved entirely at compile time.
[...]
or
3) Force x to become an array.
gawk '
BEGIN { x["dummy"] ; delete x["dummy"] }
{ x[$1] = $0 }
END { if (!length(x)) print "Nothing in the array"
for (i in x) print i,x[i]
}
'
On 12.08.2021 14:17, Janis Papanagnou wrote:
or
3) Force x to become an array.
I just recall that, I think it was Ed who suggested a less bulky form.
BEGIN { delete x[""] }
works, and it seems
BEGIN { delete x }
works as well (at least in my GNU Awk context).
In article <sf392n$gnd$1...@news-1.m-online.net>,Kenny McCormack: Would you PLEASE stop inserting political content into our conversations about the awk language! I don't care what ideology people espouse, I participate in this group to NOT have politics and ideology intrude on my conversations. Take
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
On 12.08.2021 14:17, Janis Papanagnou wrote:
or
3) Force x to become an array.
I just recall that, I think it was Ed who suggested a less bulky form.
BEGIN { delete x[""] }
works, and it seems
BEGIN { delete x }
works as well (at least in my GNU Awk context).So, the point is, it really does just boil down to: You have to ensure
that, whatever execution path your program takes, the first runtime reference to the variable is an unequivocally array context.
It strikes me that it might be a good thing for GAWK to have a "declare" statement - that would allow you to state up front that something is an array. Bash has this now, and it is actually quite useful.
--
A racist, a Nazi, and a Klansman walk into a bar...
Bartender says, "What will it be, Mr. Trump?"
In article <sf392n$gnd$1@news-1.m-online.net>,
Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
On 12.08.2021 14:17, Janis Papanagnou wrote:
or
3) Force x to become an array.
I just recall that, I think it was Ed who suggested a less bulky form.
BEGIN { delete x[""] }
works, and it seems
BEGIN { delete x }
works as well (at least in my GNU Awk context).
So, the point is, it really does just boil down to: You have to ensure
that, whatever execution path your program takes, the first runtime
reference to the variable is an unequivocally array context.
It strikes me that it might be a good thing for GAWK to have a "declare" statement - that would allow you to state up front that something is an array. Bash has this now, and it is actually quite useful.
On Thursday, 12 August 2021 at 13:27:01 UTC-4, Kenny McCormack wrote:your flames to Twitter ...
[...]Kenny McCormack: Would you PLEASE stop inserting political content into our conversations about the awk language! I don't care what ideology people espouse, I participate in this group to NOT have politics and ideology intrude on my conversations. Take
Observe:
$ gawk4 '{ x[$1] = $0 } END { if (!length(x)) print "Nothing in the array";for (i in x) print i,x[i] }'
Nothing in the array
gawk4: cmd. line:1: fatal: attempt to use scalar `x' as an array
$
In the master branch:
bash-4.2$ ./gawk '{ x[$1] = $0 } END { if (!length(x)) print "Nothing in the >array";for (i in x) print i,x[i]; print typeof(x) }' < /dev/null
Nothing in the array
array
So this problem may eventually go away. But it is safest to say 'delete x' to >avoid ambiguity.
In the master branch:
bash-4.2$ ./gawk '{ x[$1] = $0 } END { if (!length(x)) print "Nothing in
the array";for (i in x) print i,x[i]; print typeof(x) }' < /dev/null
Nothing in the array
array
So this problem may eventually go away. But it is safest to say 'delete
x' to avoid ambiguity.
Now, of course, it is clear what is going on here - and why the "fixes"
work. But what surprises me is that it (the error) happens at all. My
understanding had been that the issue (i.e., dark corner in the GAWK
language) of whether something is an array or a scalar is resolved entirely >> at compile time.
I don't think so. I think it's a pure runtime issue.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 293 |
Nodes: | 16 (2 / 14) |
Uptime: | 242:12:27 |
Calls: | 6,624 |
Files: | 12,175 |
Messages: | 5,320,202 |