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. --- awklib/eg/lib/isnumeric.awk | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 awklib/eg/lib/isnumeric.awk (limited to 'awklib/eg/lib/isnumeric.awk') diff --git a/awklib/eg/lib/isnumeric.awk b/awklib/eg/lib/isnumeric.awk new file mode 100644 index 00000000..6309e76a --- /dev/null +++ b/awklib/eg/lib/isnumeric.awk @@ -0,0 +1,14 @@ +# 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 + } +} -- 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. --- awklib/eg/lib/isnumeric.awk | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'awklib/eg/lib/isnumeric.awk') diff --git a/awklib/eg/lib/isnumeric.awk b/awklib/eg/lib/isnumeric.awk index 6309e76a..c2699022 100644 --- a/awklib/eg/lib/isnumeric.awk +++ b/awklib/eg/lib/isnumeric.awk @@ -2,13 +2,18 @@ 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. + -- cgit v1.2.3