| 1 | Nov 30, 1979: |
| 2 | |
| 3 | Awk has been modified yet again, in an attempt to make |
| 4 | its behavior more rational and predictable in the areas |
| 5 | of initialization, comparison, and type coercion. |
| 6 | Herewith what we believe the current truth to be: |
| 7 | |
| 8 | 1. Each variable and field can potentially be a string |
| 9 | or a number or both at any time. |
| 10 | When a variable is set by the assignment |
| 11 | v = expr |
| 12 | its type is set to that of expr. (This includes +=, ++, etc.) |
| 13 | An arithmetic expression is of type number, a |
| 14 | concatenation is of type string, and so on. |
| 15 | |
| 16 | If the assignment is a simple copy, as in |
| 17 | v1 = v2 |
| 18 | then the type of v1 becomes that of v2. |
| 19 | |
| 20 | 2. In comparisons, if both operands are numeric, |
| 21 | the comparison is made numerically. Otherwise, |
| 22 | operands are coerced to string if necessary, and |
| 23 | the comparison is made on strings. |
| 24 | |
| 25 | 3. The type of any expression can be coerced to |
| 26 | numeric by subterfuges (kludges?) such as |
| 27 | expr + 0 |
| 28 | and to string by |
| 29 | expr "" |
| 30 | (i.e., concatenation with a null string). |
| 31 | |
| 32 | 4. Uninitialized variables have the numeric value |
| 33 | 0 and the string value "". Accordingly, if x is |
| 34 | uninitialized, |
| 35 | if (x) ... |
| 36 | is false, and |
| 37 | if (!x) ... |
| 38 | if (x == 0) ... |
| 39 | if (x == "") ... |
| 40 | are all true. But note that |
| 41 | if (x == "0") ... |
| 42 | is false. |
| 43 | |
| 44 | 5. The type of a field is determined by context |
| 45 | when possible; for example, |
| 46 | $1++ |
| 47 | clearly implies that $1 is to be numeric, and |
| 48 | $1 = $1 "," $2 |
| 49 | implies that $1 and $2 are both to be strings. |
| 50 | Coercion will be done as needed. |
| 51 | |
| 52 | In contexts where types cannot be reliably determined, e.g., |
| 53 | if ($1 == $2) ... |
| 54 | the type of each field is determined on input by |
| 55 | inspection. All fields are strings; in addition, |
| 56 | each field that contains only a number (in the |
| 57 | sense of Fortran, say) is also considered numeric. |
| 58 | This ensures (for better or worse) that the test |
| 59 | if ($1 == $2) ... |
| 60 | will succeed on the inputs |
| 61 | 0 0.0 |
| 62 | 100 1e2 |
| 63 | +100 100 |
| 64 | 1e-3 1e-3 |
| 65 | and fail on the inputs |
| 66 | (null) 0 |
| 67 | (null) 0.0 |
| 68 | 2E-518 6E-427 |
| 69 | as we believe it should. |
| 70 | |
| 71 | Fields which are explicitly null have the string |
| 72 | value ""; they are not numeric. |
| 73 | Non-existent fields (i.e., fields past NF) are |
| 74 | treated this way too. |
| 75 | |
| 76 | As it is for fields, so it is for array elements |
| 77 | created by split(...). |
| 78 | |
| 79 | 6. There is no warranty of merchantability nor any warranty |
| 80 | of fitness for a particular purpose nor any other warranty, |
| 81 | either express or implied, as to the accuracy of the |
| 82 | enclosed materials or as to their suitability for any |
| 83 | particular purpose. Accordingly, the AWK Development |
| 84 | Task Force assumes no responsibility for their use by the |
| 85 | recipient. Further, the Task Force assumes no obligation |
| 86 | to furnish any assistance of any kind whatsoever, or to |
| 87 | furnish any additional information or documentation. |