xref: /illumos-gate/usr/src/cmd/oawk/EXPLAIN (revision 4763305e3243687c189d755d737d52205b2614ed)
1CDDL HEADER START
2
3The contents of this file are subject to the terms of the
4Common Development and Distribution License, Version 1.0 only
5(the "License").  You may not use this file except in compliance
6with the License.
7
8You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9or http://www.opensolaris.org/os/licensing.
10See the License for the specific language governing permissions
11and limitations under the License.
12
13When distributing Covered Code, include this CDDL HEADER in each
14file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15If applicable, add the following below this CDDL HEADER, with the
16fields enclosed by brackets "[]" replaced with your own identifying
17information: Portions Copyright [yyyy] [name of copyright owner]
18
19CDDL HEADER END
20
21Nov 30, 1979:
22
23Awk has been modified yet again, in an attempt to make
24its behavior more rational and predictable in the areas
25of initialization, comparison, and type coercion.
26Herewith what we believe the current truth to be:
27
281. Each variable and field can potentially be a string
29or a number or both at any time.
30When a variable is set by the assignment
31	v = expr
32its type is set to that of expr.  (This includes +=, ++, etc.)
33An arithmetic expression is of type number, a
34concatenation is of type string,  and so on.
35
36If the assignment is a simple copy, as in
37	v1 = v2
38then the type of v1 becomes that of v2.
39
402. In comparisons, if both operands are numeric,
41the comparison is made numerically.  Otherwise,
42operands are coerced to string if necessary, and
43the comparison is made on strings.
44
453. The type of any expression can be coerced to
46numeric by subterfuges (kludges?) such as
47	expr + 0
48and to string by
49	expr ""
50(i.e., concatenation with a null string).
51
524. Uninitialized variables have the numeric value
530 and the string value "".  Accordingly, if x is
54uninitialized,
55	if (x) ...
56is false, and
57	if (!x) ...
58	if (x == 0) ...
59	if (x == "") ...
60are all true.  But note that
61	if (x == "0") ...
62is false.
63
645. The type of a field is determined by context
65when possible; for example,
66	$1++
67clearly implies that $1 is to be numeric, and
68	$1 = $1 "," $2
69implies that $1 and $2 are both to be strings.
70Coercion will be done as needed.
71
72In contexts where types cannot be reliably determined, e.g.,
73	if ($1 == $2) ...
74the type of each field is determined on input by
75inspection.  All fields are strings; in addition,
76each field that contains only a number (in the
77sense of Fortran, say) is also considered numeric.
78This ensures (for better or worse) that the test
79	if ($1 == $2) ...
80will succeed on the inputs
81	0	0.0
82	100	1e2
83	+100	100
84	1e-3	1e-3
85and fail on the inputs
86	(null)	0
87	(null)	0.0
88	2E-518	6E-427
89as we believe it should.
90
91Fields which are explicitly null have the string
92value ""; they are not numeric.
93Non-existent fields (i.e., fields past NF) are
94treated this way too.
95
96As it is for fields, so it is for array elements
97created by split(...).
98
996. There is no warranty of merchantability nor any warranty
100of fitness for a particular purpose nor any other warranty,
101either express or implied, as to the accuracy of the
102enclosed materials or as to their suitability for any
103particular purpose.  Accordingly, the AWK Development
104Task Force assumes no responsibility for their use by the
105recipient.   Further, the Task Force assumes no obligation
106to furnish any assistance of any kind whatsoever, or to
107furnish any additional information or documentation.
108