DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with awk

Number or string?

Variables, fields and expressions can have both a numeric value and a string value. They take on numeric or string values according to context. For example, in the context of an arithmetic expression like

   pop += $3
pop and $3 must be treated numerically, so their values will be coerced to numeric type if necessary.

In a string context like

   print $1 ":" $2

$1 and $2 must be strings to be concatenated, so they will be coerced if necessary.

In an assignment v =e or v op=e, the type of v becomes the type of e. In an ambiguous context like

   $1 == $2
the type of the comparison depends on whether the fields are numeric or string, and this can only be determined when the program runs; it may well differ from record to record.

In comparisons, if both operands are numeric, the comparison is numeric; otherwise, operands are coerced to strings, and the comparison is made on the string values. All field variables are of type string; in addition, each field that contains only a number is also considered numeric. This determination is done at run time. For example, the comparison ``"$1 == $2"'' will succeed on any pair of the inputs

   1    1.0    +1    0.1e+1    10E-1   001    .01E2
but will fail on the inputs
   (null)	0
   (null)	0.0
   0a	0
   1e50	1.0e50

There are two idioms for coercing an expression of one type to the other:


number 
concatenate a null string to a number to coerce it to type string

string + 0
add zero to a string to coerce it to type numeric
Thus, to force a string comparison between two fields, use
   $1 "" == $2 ""

The numeric value of a string is the value of any prefix of the string that looks numeric; thus the value of 12.34x is 12.34, while the value of x12.34 is zero. The string value of an arithmetic expression is computed by formatting the string with the output format conversion OFMT.

Uninitialized variables have numeric value 0 and string value "". Nonexistent fields and fields that are explicitly null have only the string value ""; they are not numeric.


Next topic: Control flow statements
Previous topic: Field variables

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005