From 1bf8a67c114568b307c1df6dfe2042e2a3eab49b Mon Sep 17 00:00:00 2001 From: "Andrew J. Schorr" Date: Tue, 22 Jun 2021 13:45:25 -0400 Subject: Add isnumeric function. --- doc/gawktexi.in | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'doc/gawktexi.in') diff --git a/doc/gawktexi.in b/doc/gawktexi.in index 37ecbc9f..81a24079 100644 --- a/doc/gawktexi.in +++ b/doc/gawktexi.in @@ -803,6 +803,8 @@ particular records in a file and perform operations upon them. once. * Shell Quoting:: A function to quote strings for the shell. +* Isnumeric Function:: A function to test whether a value + is numeric. * Data File Management:: Functions for managing command-line data files. * Filetrans Function:: A function for handling data file @@ -21374,6 +21376,7 @@ programming use. * Getlocaltime Function:: A function to get formatted times. * Readfile Function:: A function to read an entire file at once. * Shell Quoting:: A function to quote strings for the shell. +* Isnumeric Function:: A function to test whether a value is numeric. @end menu @node Strtonum Function @@ -22164,6 +22167,35 @@ function shell_quote(s, # parameter @c endfile @end example +@node Isnumeric Function +@subsection Checking whether a value is numeric + +A frequent programming question is how to ascertain whether a value is numeric. +This can be solved by using this example function @code{isnumeric()}, which +employs the trick of converting a string value to user input by using the +@code{split()} function: + +@cindex @code{isnumeric()} user-defined function +@cindex user-defined @subentry function @subentry @code{isnumeric()} +@example +@c file eg/lib/isnumeric.awk +# isnumeric --- check whether a value is numeric + +function isnumeric(x, f) +@{ + switch (typeof(x)) @{ + case "strnum": + case "number": + return 1 + case "string": + return (split(x, f, " ") == 1) && (typeof(f[1]) == "strnum") + default: + return 0 + @} +@} +@c endfile +@end example + @node Data File Management @section @value{DDF} Management -- cgit v1.2.3 From 98ef29fdcadffc0a05c91883c4ab8809a8b0d441 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 23 Jun 2021 20:30:13 +0300 Subject: More doc updates. --- doc/gawktexi.in | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'doc/gawktexi.in') diff --git a/doc/gawktexi.in b/doc/gawktexi.in index 81a24079..236ef638 100644 --- a/doc/gawktexi.in +++ b/doc/gawktexi.in @@ -22168,7 +22168,7 @@ function shell_quote(s, # parameter @end example @node Isnumeric Function -@subsection Checking whether a value is numeric +@subsection Checking Whether A Value Is Numeric A frequent programming question is how to ascertain whether a value is numeric. This can be solved by using this example function @code{isnumeric()}, which @@ -22183,16 +22183,21 @@ employs the trick of converting a string value to user input by using the function isnumeric(x, f) @{ - switch (typeof(x)) @{ - case "strnum": - case "number": - return 1 - case "string": - return (split(x, f, " ") == 1) && (typeof(f[1]) == "strnum") - default: - return 0 - @} + switch (typeof(x)) @{ + case "strnum": + case "number": + return 1 + case "string": + return (split(x, f, " ") == 1) && (typeof(f[1]) == "strnum") + default: + return 0 + @} @} + +Please note that leading or trailing white space is disregarded in deciding +whether a value is numeric or not, so if it matters to you, you may want +to add an additional check for that. + @c endfile @end example @@ -23037,8 +23042,8 @@ $ @kbd{awk -f getopt.awk -v _getopt_test=1 -- -a \} @print{} c = , Optarg = <> @print{} c = , Optarg = <> @print{} non-option arguments: -@print{} ARGV[8] = -@print{} ARGV[9] = +@print{} ARGV[8] = +@print{} ARGV[9] = @end example In all the runs, the first @option{--} terminates the arguments to @@ -25235,7 +25240,7 @@ look nice on the page: @end ignore @c file eg/prog/split.awk -function usage( common) +function usage( common) @{ common = "[-a suffix-len] [file [outname]]" printf("usage: split [-l count] %s\n", common) > "/dev/stderr" @@ -29068,7 +29073,7 @@ main(void) printf("%d\n", x + y); return 0; @} -$ @kbd{cc -O add.c -o add} @ii{Compile the program} +$ @kbd{cc -O add.c -o add} @ii{Compile the program} @end example You could then write an exceedingly simple @command{gawk} program -- cgit v1.2.3