Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One of the most frustrating AWK programs

Status
Not open for further replies.

d0vgan

Programmer
Jul 30, 2007
4
0
0
UA
Recently I wrote the following fragment in AWK by accident:

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
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:

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.
 
There is nothing wrong with the script below, save that it is an example of obfuscated awk scripting.
Code:
A = "a"
B = "b"

{
}

END {
  print A, B
}
Moreover, it is semantically different from the script that you've posted although it looks syntactically similar to the one you're talking about.
 
I do understand that is a valid AWK program, but I tried to highlight the fact that the actual meaning of

Code:
A = "a"

according to AWK's interpretation, is

Code:
(A = "a") != 0 { print $0 }

and it is so unlogical that it is almost meaningless.

I mean, is there any sense to write such a condition that is always true and silently invokes "print $0"?
Personally I do not see any sense in writing this intentionally. In my opinion, something like this can be written only by mistake.

I would understand if it was e.g.

Code:
A == "a"

In such case it would be an obvious condition, that intentionally checks a variable A against the value of "a", with the default action of "print $0" assumed.
But what is the sense to do the same in case of assignment? Assignment, Carl!!!
 
Your frustration is valid, but that is the purpose of writing obfuscated programs. Anyways I think we have beaten this dead animal far too much. It'd have been nice to put the comparison operator instead of the assignment one but since the latter evaluates to true for each line, they're all printed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top