In article <
[email protected]>,
Mary Karr <
[email protected]> wrote:
This code prints "yes" in 4.1
awk 'BEGIN{token = "firstname"; if(token ~ (/^lastname|^firstname/)) {print "yes"} }'
..and in 5.1 prints nothing
Confirmed. In 4.1.4, it prints "yes"; in 5.0.1 it prints nothing.
The code is user error, there should be no parens around the //.
It's confusing why the versions have different results. Did 4.1
auto-detect the problem?
Actually, it looks like it is a bug fixed in 5.x.
The previous (4.x) behavior is actually incorrect.
Here's my explanation (which you may or may not already know). Basically,
AWK (specifically, GAWK) tries very hard to come up with a legal
interpretation of whatever you throw at it. Kind of like when a human
says something nonsensical to you; you try very hard to come up with an interpretation of what they said that makes sense. Only if that is not possible do you tell them that they are speaking gibberish.
What is happening is that when you put a bare regular expression in parens,
it then gets evaluated as either 0 or 1, depending on whether or not the
reg exp matches (i.e., is contained in) $0. The following demonstrates how
to change your program so that a current version of GAWK will print "yes":
$ /usr/bin/gawk 'BEGIN{$0="lastname";token = "first1ame"; if(token ~ (/^lastname|^firstname/)) {print "yes"} }'
yes
$
--
Mike Huckabee has yet to consciously uncouple from Josh Duggar.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)