diff options
Diffstat (limited to 'doc/gawk.texi')
-rw-r--r-- | doc/gawk.texi | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/doc/gawk.texi b/doc/gawk.texi index d0832b5c..b053b171 100644 --- a/doc/gawk.texi +++ b/doc/gawk.texi @@ -810,6 +810,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 @@ -22493,6 +22495,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 @@ -23283,6 +23286,40 @@ 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 + @} +@} + +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 + @node Data File Management @section @value{DDF} Management @@ -24154,8 +24191,8 @@ $ @kbd{awk -f getopt.awk -v _getopt_test=1 -- -a \} @print{} c = <otherd>, Optarg = <> @print{} c = <otherc>, Optarg = <> @print{} non-option arguments: -@print{} ARGV[8] = <arg1> -@print{} ARGV[9] = <arg2> +@print{} ARGV[8] = <arg1> +@print{} ARGV[9] = <arg2> @end example In all the runs, the first @option{--} terminates the arguments to @@ -26352,7 +26389,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" @@ -30229,7 +30266,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 |