manpagez: man pages & more
info gawk
Home | html | info | man

File: gawk.info,  Node: Boolean Typed Values,  Next: Array Sorting,  Prev: Nondecimal Data,  Up: Advanced Features

12.2 Boolean Typed Values
=========================

Scalar values in 'awk' are either numbers or strings.  'gawk' also
supports values of type 'regexp' (*note Strong Regexp Constants::).

   As described in *note Truth Values::, Boolean values in 'awk' don't
have a separate type: a value counts as "true" if it is nonzero or
non-null, and as "false" otherwise.

   When interchanging data with languages that do have a real Boolean
type, using a standard format such as JSON or XML, the lack of a true
Boolean type in 'awk' is problematic.  (See, for example, the 'json'
extension provided by the 'gawkextlib' project
(https://sourceforge.net/projects/gawkextlib).)

   It's easy to import Boolean data into 'awk', but then the fact that
it was originally Boolean is lost.  Exporting data is even harder;
there's no way to indicate that a value is really Boolean.

   To solve this problem, 'gawk' provides a function named 'mkbool()'.
It takes one argument, which is any 'awk' expression, and it returns a
value of Boolean type.

   The returned values are normal 'awk' numeric values, with values of
either one or zero, depending upon the truth value of the original
expression passed in the call to 'mkbool()'.

   The 'typeof()' function (*note Type Functions::) returns
'"number|bool"' for these values.

   Thus Boolean-typed values _are_ numbers as far as 'gawk' is
concerned, except that extension code can treat them as Booleans if
desired.

   While it would have been possible to add two new built-in variables
of Boolean type named 'TRUE' and 'FALSE', doing so would undoubtedly
have broken many existing 'awk' programs.  Instead, having a "generator"
function that creates Boolean values gives flexibility, without breaking
as much existing code.

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.