21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5: Expressions 87lvalue--This expression is like ‘lvalue++’, but instead of adding, it subtracts. It decrementslvalue. The value of the expression is the old value of lvalue.Advanced Notes: Operator Evaluation OrderDoctor, doctor! It hurts when I do this!So don’t do that!Groucho MarxWhat happens for something like the following?b = 6print b += b++Or something even stranger?b = 6b += ++b + b++print bIn other words, when do the various side effects prescribed by the postfix operators(‘b++’) take effect? When side effects happen is implementation defined. In other words, itis up to the particular version of awk. The result for the first example may be 12 or 13, andfor the second, it may be 22 or 23.In short, doing things like this is not recommended and definitely not anything that youcan rely upon for portability. You should avoid such things in your own programs.5.9 True and False in awkMany programming languages have a special representation for the concepts of “true” and“false.” Such languages usually use the special constants true and false, or perhaps theiruppercase equivalents. However, awk is different. It borrows a very simple concept of trueand false from C. In awk, any nonzero numeric value or any nonempty string value is true.Any other value (zero or the null string "") is false. The following program prints ‘A strangetruth value’ three times:BEGIN {if (3.1415927)print "A strange truth value"if ("Four Score And Seven Years Ago")print "A strange truth value"if (j = 57)print "A strange truth value"}There is a surprising consequence of the “nonzero or non-null” rule: the string constant"0" is actually true, because it is non-null.5.10 Variable Typing and Comparison ExpressionsThe Guide is definitive. Reality is frequently inaccurate.The Hitchhiker’s Guide to the Galaxy

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

Saved successfully!

Ooh no, something went wrong!