Page 1 of 1
How to test a variable to be numeric
Posted: 16 Apr 2023 17:25
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

Re: How to test a variable to be numeric
Posted: 16 Apr 2023 17:39
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...
Re: How to test a variable to be numeric
Posted: 16 Apr 2023 19:47
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...)
Re: How to test a variable to be numeric
Posted: 16 Apr 2023 19:57
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";
}
Re: How to test a variable to be numeric
Posted: 16 Apr 2023 21:15
by PeterH
OK - at least what we think about "how the script sees the vars" seems to be identical
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

)
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

,
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...
Re: How to test a variable to be numeric
Posted: 17 Apr 2023 01:18
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.