Recently I wrote the following fragment in AWK by accident:
And the result completely blowed my mind up.
Just try to run this code against e.g. the following input file:
and enjoy the result.
The AWK code above was actually a part of a bigger program - and, believe me, I spent an hour (!) in frustration, trying to understand what the heck was happening.
The issue with this code is that in fact it does absolutely, completely and fundamentally different things than you may expect from the code that looks like this.
Just compare it with the following, a little bit modified, code:
Looks pretty similar to the first code fragment, isn't it?
But the behavior is absolutely, completely and fundamentally different!
And here is what I am thinking about this:
- why the first code fragment is silently compiled by AWK without any warning???
I mean, what is the real use-case of the code like that? Would you really think that someone, who is in his own mind, would intentionally write the code like that, assuming the behavior that AWK provides for that? I would only think that someone could write such code intentionally with the only purpose of frustration and madness of everyone else who would use that code.
What do you think?
In my opinion, this question is worth to be raised to GAWK team with regards to provide at least a warning for such code.
Code:
A = "a"
B = "b"
{
}
END {
print A, B
}
And the result completely blowed my mind up.
Just try to run this code against e.g. the following input file:
Code:
1
2
3
The AWK code above was actually a part of a bigger program - and, believe me, I spent an hour (!) in frustration, trying to understand what the heck was happening.
The issue with this code is that in fact it does absolutely, completely and fundamentally different things than you may expect from the code that looks like this.
Just compare it with the following, a little bit modified, code:
Code:
BEGIN {
A = "a"
B = "b"
}
{
}
END {
print A, B
}
Looks pretty similar to the first code fragment, isn't it?
But the behavior is absolutely, completely and fundamentally different!
And here is what I am thinking about this:
- why the first code fragment is silently compiled by AWK without any warning???
I mean, what is the real use-case of the code like that? Would you really think that someone, who is in his own mind, would intentionally write the code like that, assuming the behavior that AWK provides for that? I would only think that someone could write such code intentionally with the only purpose of frustration and madness of everyone else who would use that code.
What do you think?
In my opinion, this question is worth to be raised to GAWK team with regards to provide at least a warning for such code.