How to test a variable to be numeric

Discuss and share scripts and script files...
Post Reply
PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

How to test a variable to be numeric

Post by PeterH »

Does someone have a tip?
Problem: a parameter can contain different strings, or a number (integer). And a number needs special handling.

Maybe there should be a function vartype(input), that returns codes like:
'empty', 'string', 'integer', 'float'?
Other ideas? 'undefined'/'unset' [see isset()]?) If an undefined var is spcified, it's none of above.
OK: maybe 'array'? The array *iis* kind of type - only it's elements have a data-type like 'string'.
better: 'array-i[ndexed]' and 'array-a[ssociative]' ?

Come on - didn't have a wish for a long time :whistle:
Win11 Pro 223H2 Gerrman

highend
Posts: 13317
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to test a variable to be numeric

Post by highend »

Test for numeric? regex but you'd never know if it is still a string....

$a = 15; // int
$a = "15" // string

Don should build this in...
One of my scripts helped you out? Please donate via Paypal

PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

Re: How to test a variable to be numeric

Post by PeterH »

Hm - I thought that in XY *all* variables are strings - but if all chars are 0-9, +, - they can be treated as int, and calculated with. And if theres a dot it must be float. I hope I'm right?

Can you show that for $a=15; and $b="15"; there's any difference between both? At least if you add both to $c=11; there seems to be none. And you can use both as index for gettoken(), can't you? Bad if you try with $d="a5";
This is what I meant with my question.

And sorry: *much* more than 20 years ago I decided not to learn regex - and will not change that to my old days.
(Had enough programming languages. And *very* little use for regex on mainframe then...)
Win11 Pro 223H2 Gerrman

highend
Posts: 13317
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: How to test a variable to be numeric

Post by highend »

Can you show that for $a=15; and $b="15"; there's any difference between both?
Afaik not via scripting. The initial quotation marks are stripped once it reaches a function (to test that)

If you don't care if a value can be string or an int / float (at least from what it contains)...

Code: Select all

    text vartype("");   // empty
    text vartype(15.4); // float
    text vartype(.4);   // float
    text vartype(15);   // int
    text vartype("a");  // string

function vartype($value) {
    if ($value == "") { return "empty"; }
    if (regexmatches($value, "^[0-9.,]+?$")) {
        if (regexmatches($value, "(\.|,)")) { return "float"; }
        return "int";
    }
    return "string";
}
One of my scripts helped you out? Please donate via Paypal

PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

Re: How to test a variable to be numeric

Post by PeterH »

OK - at least what we think about "how the script sees the vars" seems to be identical :tup:

I could have written a little routine, a loop that checks every char for digit etc, but really didn't want to.
(Though it would have been faster for me than to learn regex :titter: )

As your regex-code looks much better I have copied it to my .xyi-library. Thanks for that!
(Hoping I can somewhen replace it by an XY-internal version :mrgreen: ,
maybe knowing about arrays - it *should* be possible to check a var / a parm for being array)

So it also seems we are both thinking that array support (still) lacks of many things...
Win11 Pro 223H2 Gerrman

klownboy
Posts: 4140
Joined: 28 Feb 2012 19:27

Re: How to test a variable to be numeric

Post by klownboy »

I'll save that function too highend. Thanks. I needed to distinguish a number from a string in a script the other day. In my specific case the number could only be 0-9 so,
if($days LikeI "#") { bla bla bla was all I needed. An XYplorer built-in function would be welcome.
Windows 11, 23H2 Build 22631.3447 at 100% 2560x1440

Post Reply