aboutsummaryrefslogtreecommitdiffstats
path: root/doc/gawktexi.in
diff options
context:
space:
mode:
authorAndrew J. Schorr <aschorr@telemetry-investments.com>2021-06-22 13:45:25 -0400
committerAndrew J. Schorr <aschorr@telemetry-investments.com>2021-06-22 13:45:25 -0400
commit1bf8a67c114568b307c1df6dfe2042e2a3eab49b (patch)
treed02018ca12bf82104f90804df4b056d44b823a4f /doc/gawktexi.in
parente7706feed9aac915db7307ad59d3908a8952eaf0 (diff)
downloadegawk-1bf8a67c114568b307c1df6dfe2042e2a3eab49b.tar.gz
egawk-1bf8a67c114568b307c1df6dfe2042e2a3eab49b.tar.bz2
egawk-1bf8a67c114568b307c1df6dfe2042e2a3eab49b.zip
Add isnumeric function.
Diffstat (limited to 'doc/gawktexi.in')
-rw-r--r--doc/gawktexi.in32
1 files changed, 32 insertions, 0 deletions
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