21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5: Expressions 77the same meaning as if it appeared in a pattern, i.e., ‘($0 ~ /foo/)’ See Section 6.1.2[Expressions as Patterns], page 96. This means that the following two code segments:if ($0 ~ /barfly/ || $0 ~ /camelot/)print "found"and:if (/barfly/ || /camelot/)print "found"are exactly equivalent. One rather bizarre consequence of this rule is that the followingBoolean expression is valid, but does not do what the user probably intended:# note that /foo/ is on the left of the ~if (/foo/ ~ $1) print "found foo"This code is “obviously” testing $1 for a match against the regexp /foo/. But in fact, theexpression ‘/foo/ ~ $1’ actually means ‘($0 ~ /foo/) ~ $1’. In other words, first matchthe input record against the regexp /foo/. The result is either zero or one, depending uponthe success or failure of the match. That result is then matched against the first field inthe record. Because it is unlikely that you would ever really want to make this kind of test,gawk issues a warning when it sees this construct in a program. Another consequence ofthis rule is that the assignment statement:matches = /foo/assigns either zero or one to the variable matches, depending upon the contents of thecurrent input record. This feature of the language has never been well documented untilthe POSIX specification.Constant regular expressions are also used as the first argument for the gensub, sub, andgsub functions, and as the second argument of the match function (see Section 8.1.3 [String-Manipulation Functions], page 132). Modern implementations of awk, including gawk, allowthe third argument of split to be a regexp constant, but some older implementations donot. This can lead to confusion when attempting to use regexp constants as arguments touser-defined functions (see Section 8.2 [User-Defined Functions], page 153). For example:function mysub(pat, repl, str, global){if (global)gsub(pat, repl, str)elsesub(pat, repl, str)return str}{...text = "hi! hi yourself!"mysub(/hi/, "howdy", text, 1)...}In this example, the programmer wants to pass a regexp constant to the user-definedfunction mysub, which in turn passes it on to either sub or gsub. However, what really

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!