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/gawk.texi | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'doc/gawk.texi') diff --git a/doc/gawk.texi b/doc/gawk.texi index 2478cf98..fb71bb68 100644 --- a/doc/gawk.texi +++ b/doc/gawk.texi @@ -808,6 +808,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 @@ -22462,6 +22464,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 @@ -23252,6 +23255,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/gawk.texi | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'doc/gawk.texi') diff --git a/doc/gawk.texi b/doc/gawk.texi index fb71bb68..0372897f 100644 --- a/doc/gawk.texi +++ b/doc/gawk.texi @@ -23256,7 +23256,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 @@ -23271,16 +23271,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 @@ -24155,8 +24160,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 @@ -26353,7 +26358,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" @@ -30186,7 +30191,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