xref: /illumos-gate/usr/src/cmd/oawk/EXPLAIN (revision 4774dff6)
1*7c478bd9Sstevel@tonic-gateCDDL HEADER START
2*7c478bd9Sstevel@tonic-gate
3*7c478bd9Sstevel@tonic-gateThe contents of this file are subject to the terms of the
4*7c478bd9Sstevel@tonic-gateCommon Development and Distribution License, Version 1.0 only
5*7c478bd9Sstevel@tonic-gate(the "License").  You may not use this file except in compliance
6*7c478bd9Sstevel@tonic-gatewith the License.
7*7c478bd9Sstevel@tonic-gate
8*7c478bd9Sstevel@tonic-gateYou can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7c478bd9Sstevel@tonic-gateor http://www.opensolaris.org/os/licensing.
10*7c478bd9Sstevel@tonic-gateSee the License for the specific language governing permissions
11*7c478bd9Sstevel@tonic-gateand limitations under the License.
12*7c478bd9Sstevel@tonic-gate
13*7c478bd9Sstevel@tonic-gateWhen distributing Covered Code, include this CDDL HEADER in each
14*7c478bd9Sstevel@tonic-gatefile and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7c478bd9Sstevel@tonic-gateIf applicable, add the following below this CDDL HEADER, with the
16*7c478bd9Sstevel@tonic-gatefields enclosed by brackets "[]" replaced with your own identifying
17*7c478bd9Sstevel@tonic-gateinformation: Portions Copyright [yyyy] [name of copyright owner]
18*7c478bd9Sstevel@tonic-gate
19*7c478bd9Sstevel@tonic-gateCDDL HEADER END
20*7c478bd9Sstevel@tonic-gate
21*7c478bd9Sstevel@tonic-gateNov 30, 1979:
22*7c478bd9Sstevel@tonic-gate
23*7c478bd9Sstevel@tonic-gateAwk has been modified yet again, in an attempt to make
24*7c478bd9Sstevel@tonic-gateits behavior more rational and predictable in the areas
25*7c478bd9Sstevel@tonic-gateof initialization, comparison, and type coercion.
26*7c478bd9Sstevel@tonic-gateHerewith what we believe the current truth to be:
27*7c478bd9Sstevel@tonic-gate
28*7c478bd9Sstevel@tonic-gate1. Each variable and field can potentially be a string
29*7c478bd9Sstevel@tonic-gateor a number or both at any time.
30*7c478bd9Sstevel@tonic-gateWhen a variable is set by the assignment
31*7c478bd9Sstevel@tonic-gate	v = expr
32*7c478bd9Sstevel@tonic-gateits type is set to that of expr.  (This includes +=, ++, etc.)
33*7c478bd9Sstevel@tonic-gateAn arithmetic expression is of type number, a
34*7c478bd9Sstevel@tonic-gateconcatenation is of type string,  and so on.
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gateIf the assignment is a simple copy, as in
37*7c478bd9Sstevel@tonic-gate	v1 = v2
38*7c478bd9Sstevel@tonic-gatethen the type of v1 becomes that of v2.
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate2. In comparisons, if both operands are numeric,
41*7c478bd9Sstevel@tonic-gatethe comparison is made numerically.  Otherwise,
42*7c478bd9Sstevel@tonic-gateoperands are coerced to string if necessary, and
43*7c478bd9Sstevel@tonic-gatethe comparison is made on strings.
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate3. The type of any expression can be coerced to
46*7c478bd9Sstevel@tonic-gatenumeric by subterfuges (kludges?) such as
47*7c478bd9Sstevel@tonic-gate	expr + 0
48*7c478bd9Sstevel@tonic-gateand to string by
49*7c478bd9Sstevel@tonic-gate	expr ""
50*7c478bd9Sstevel@tonic-gate(i.e., concatenation with a null string).
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate4. Uninitialized variables have the numeric value
53*7c478bd9Sstevel@tonic-gate0 and the string value "".  Accordingly, if x is
54*7c478bd9Sstevel@tonic-gateuninitialized,
55*7c478bd9Sstevel@tonic-gate	if (x) ...
56*7c478bd9Sstevel@tonic-gateis false, and
57*7c478bd9Sstevel@tonic-gate	if (!x) ...
58*7c478bd9Sstevel@tonic-gate	if (x == 0) ...
59*7c478bd9Sstevel@tonic-gate	if (x == "") ...
60*7c478bd9Sstevel@tonic-gateare all true.  But note that
61*7c478bd9Sstevel@tonic-gate	if (x == "0") ...
62*7c478bd9Sstevel@tonic-gateis false.
63*7c478bd9Sstevel@tonic-gate
64*7c478bd9Sstevel@tonic-gate5. The type of a field is determined by context
65*7c478bd9Sstevel@tonic-gatewhen possible; for example,
66*7c478bd9Sstevel@tonic-gate	$1++
67*7c478bd9Sstevel@tonic-gateclearly implies that $1 is to be numeric, and
68*7c478bd9Sstevel@tonic-gate	$1 = $1 "," $2
69*7c478bd9Sstevel@tonic-gateimplies that $1 and $2 are both to be strings.
70*7c478bd9Sstevel@tonic-gateCoercion will be done as needed.
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gateIn contexts where types cannot be reliably determined, e.g.,
73*7c478bd9Sstevel@tonic-gate	if ($1 == $2) ...
74*7c478bd9Sstevel@tonic-gatethe type of each field is determined on input by
75*7c478bd9Sstevel@tonic-gateinspection.  All fields are strings; in addition,
76*7c478bd9Sstevel@tonic-gateeach field that contains only a number (in the
77*7c478bd9Sstevel@tonic-gatesense of Fortran, say) is also considered numeric.
78*7c478bd9Sstevel@tonic-gateThis ensures (for better or worse) that the test
79*7c478bd9Sstevel@tonic-gate	if ($1 == $2) ...
80*7c478bd9Sstevel@tonic-gatewill succeed on the inputs
81*7c478bd9Sstevel@tonic-gate	0	0.0
82*7c478bd9Sstevel@tonic-gate	100	1e2
83*7c478bd9Sstevel@tonic-gate	+100	100
84*7c478bd9Sstevel@tonic-gate	1e-3	1e-3
85*7c478bd9Sstevel@tonic-gateand fail on the inputs
86*7c478bd9Sstevel@tonic-gate	(null)	0
87*7c478bd9Sstevel@tonic-gate	(null)	0.0
88*7c478bd9Sstevel@tonic-gate	2E-518	6E-427
89*7c478bd9Sstevel@tonic-gateas we believe it should.
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gateFields which are explicitly null have the string
92*7c478bd9Sstevel@tonic-gatevalue ""; they are not numeric.
93*7c478bd9Sstevel@tonic-gateNon-existent fields (i.e., fields past NF) are
94*7c478bd9Sstevel@tonic-gatetreated this way too.
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gateAs it is for fields, so it is for array elements
97*7c478bd9Sstevel@tonic-gatecreated by split(...).
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate6. There is no warranty of merchantability nor any warranty
100*7c478bd9Sstevel@tonic-gateof fitness for a particular purpose nor any other warranty,
101*7c478bd9Sstevel@tonic-gateeither express or implied, as to the accuracy of the
102*7c478bd9Sstevel@tonic-gateenclosed materials or as to their suitability for any
103*7c478bd9Sstevel@tonic-gateparticular purpose.  Accordingly, the AWK Development
104*7c478bd9Sstevel@tonic-gateTask Force assumes no responsibility for their use by the
105*7c478bd9Sstevel@tonic-gaterecipient.   Further, the Task Force assumes no obligation
106*7c478bd9Sstevel@tonic-gateto furnish any assistance of any kind whatsoever, or to
107*7c478bd9Sstevel@tonic-gatefurnish any additional information or documentation.
108