This code prints "yes" in 4.1
awk 'BEGIN{token = "firstname"; if(token ~ (/^lastname|^firstname/)) {print "yes"} }'
..and in 5.1 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?
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":
On 2023-05-29, Kenny McCormack <gazelle@shell.xmission.com> wrote:
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":
Awk is "weird" that way. Ostensibly, it is based on C, but brings
in various quirks, includiing whitespace sensitivities in the middle
of expressions.
In article <20230529111746.484@kylheku.com>,
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
On 2023-05-29, Kenny McCormack <gazelle@shell.xmission.com> wrote:
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": >>Awk is "weird" that way. Ostensibly, it is based on C, but brings
in various quirks, includiing whitespace sensitivities in the middle
of expressions.
Note, BTW, that if you change the slashes to quotes - i.e., put:
token ~ ("^lastname|^firstname"))
the problem goes away (though other problems may appear).
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 920 |
Nodes: | 10 (0 / 10) |
Uptime: | 102:45:55 |
Calls: | 12,189 |
Calls today: | 1 |
Files: | 186,527 |
Messages: | 2,237,485 |