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