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