SC: verifying user input to be a number - HOW?
-
autocart
- Posts: 1386
- Joined: 26 Sep 2013 15:22
SC: verifying user input to be a number - HOW?
Hi all,
How would one go about verifying user input - function: input(topic, [notes], [default], [style=s|m|w], [cancel], [width=600], [height=400]) - to see whether the return value, which obiously is a sting, represents an integer number and has no other chars in it? It seems that XY-scripting does not offer a simple method to do that, or did I overlook something? Thx in advance.
Regards, Stephan
How would one go about verifying user input - function: input(topic, [notes], [default], [style=s|m|w], [cancel], [width=600], [height=400]) - to see whether the return value, which obiously is a sting, represents an integer number and has no other chars in it? It seems that XY-scripting does not offer a simple method to do that, or did I overlook something? Thx in advance.
Regards, Stephan
[AHK] redirecting Windows Explorer to XY, [XYS] Mini Tree with open tabs (cur loc expanded, tab folders highlighted), [AHK] customInlineRenameKeys, [AHK] clipboardHelper_and_XYEscToList
-
bdeshi
- Posts: 4256
- Joined: 12 Mar 2014 17:27
- Location: Asteroid B-612
- Contact:
Re: SC: verifying user input to be a number - HOW?
Code: Select all
set $num;
while ($num UnLikeI "[0-9]") || (strlen($num) != 1){
$num = input("Enter a single numerical character","Or face the horror or the Ever-looping Inputbox!!",,s,"CancelIsNotAnEscape");
}EDITed out an embarrassing mistake in the snippet.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]
[ this user is asleep ]
-
autocart
- Posts: 1386
- Joined: 26 Sep 2013 15:22
Re: SC: verifying user input to be a number - HOW?
Thank you, Sammay,
I am glad that I overlooked something, namely the like / unlike operator.
I am glad that I overlooked something, namely the like / unlike operator.
[AHK] redirecting Windows Explorer to XY, [XYS] Mini Tree with open tabs (cur loc expanded, tab folders highlighted), [AHK] customInlineRenameKeys, [AHK] clipboardHelper_and_XYEscToList
-
autocart
- Posts: 1386
- Joined: 26 Sep 2013 15:22
Re: SC: verifying user input to be a number - HOW?
However, it only works with a fix number of chars.
It is not flexible to work with "2 or "22" or "222" or "2222" or ....
It is not flexible to work with "2 or "22" or "222" or "2222" or ....
[AHK] redirecting Windows Explorer to XY, [XYS] Mini Tree with open tabs (cur loc expanded, tab folders highlighted), [AHK] customInlineRenameKeys, [AHK] clipboardHelper_and_XYEscToList
-
highend
- Posts: 14953
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: SC: verifying user input to be a number - HOW?
Code: Select all
$input = input("Enter a numerical character");
$onlyNumber = regexmatches($input, "^\d+$");
if ($onlyNumber) {
text "yeah!";
}One of my scripts helped you out? Please donate via Paypal
-
autocart
- Posts: 1386
- Joined: 26 Sep 2013 15:22
Re: SC: verifying user input to be a number - HOW?
Perfect, thx, highendhighend wrote:Code: Select all
$input = input("Enter a numerical character"); $onlyNumber = regexmatches($input, "^\d+$"); if ($onlyNumber) { text "yeah!"; }
[AHK] redirecting Windows Explorer to XY, [XYS] Mini Tree with open tabs (cur loc expanded, tab folders highlighted), [AHK] customInlineRenameKeys, [AHK] clipboardHelper_and_XYEscToList
-
binocular222
- Posts: 1423
- Joined: 04 Nov 2008 05:35
- Location: Win11, Win10, 100% Scaling
Re: SC: verifying user input to be a number - HOW?
easyway:
Code: Select all
$input = input("Enter a numerical character");
if($input == 0 OR $input * 1 != 0) {echo Number}; Else {echo Text}I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488
-
autocart
- Posts: 1386
- Joined: 26 Sep 2013 15:22
Re: SC: verifying user input to be a number - HOW?
yep, seems to work also. thx, binobinocular222 wrote:easyway:Code: Select all
$input = input("Enter a numerical character"); if($input == 0 OR $input * 1 != 0) {echo Number}; Else {echo Text}
actually I did think of that in a similar way at first, but discarded the idea w/o trying because in comparisons (like 1 < "a", which results into 1 respectively "true") the string is (obviously) "turned into" a number.
Plus the help file says:
So I wrongly concluded that it would be the same with multiplication.Strings are converted to numbers as possible (just like in comparisons).
$a=""; $b="2"; echo $a + $b; // = 2
$a="1a"; $b="2b"; echo $a + $b; // = 3
BTW, as I just found out (by trying it out): The help file is wrong in this point. The result in this example is not 3 but 0.
EDIT:
actually the help file says about comparisions:
Which is weird, since 1 < "1a" results into 1 resp. "true". But 2 < "1a" results into 0 resp. "false". WEIRD!!!!!!Strings and Numbers
If you compare two numerical strings, they are compared as integers:
msg ("2" < "10"); // -> true! (both parts are numeric)
msg ("2" < "10a"); // -> false! (only one parts numeric)
msg ("2a" < "10a"); // -> false! (both parts are non-numeric)
[AHK] redirecting Windows Explorer to XY, [XYS] Mini Tree with open tabs (cur loc expanded, tab folders highlighted), [AHK] customInlineRenameKeys, [AHK] clipboardHelper_and_XYEscToList
-
PeterH
- Posts: 2827
- Joined: 21 Nov 2005 20:39
- Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%
Re: SC: verifying user input to be a number - HOW?
I think that's OK: it's a string compare, that is character for character, left to right, up to the first difference.autocart wrote:actually the help file says about comparisions:Which is weird, since 1 < "1a" results into 1 resp. "true". But 2 < "1a" results into 0 resp. "false". WEIRD!!!!!!Strings and Numbers
If you compare two numerical strings, they are compared as integers:
msg ("2" < "10"); // -> true! (both parts are numeric)
msg ("2" < "10a"); // -> false! (only one parts numeric)
msg ("2a" < "10a"); // -> false! (both parts are non-numeric)
so 1... is always < than 2..., no matter what follows 1st character.
2 is > 10a because 2 > 1
So all correct?
-
autocart
- Posts: 1386
- Joined: 26 Sep 2013 15:22
Re: SC: verifying user input to be a number - HOW?
i somehow makes sense what you say, Peter. Thx.
Then, in a comparison of "real" number against non-numeric string, is the "real" number converted into a string? Obiously yes, or am I wrong?
I dunno why this is so confusing to me these days. Sorry to all, for cluttering the forum.
Then, in a comparison of "real" number against non-numeric string, is the "real" number converted into a string? Obiously yes, or am I wrong?
I dunno why this is so confusing to me these days. Sorry to all, for cluttering the forum.
[AHK] redirecting Windows Explorer to XY, [XYS] Mini Tree with open tabs (cur loc expanded, tab folders highlighted), [AHK] customInlineRenameKeys, [AHK] clipboardHelper_and_XYEscToList
-
PeterH
- Posts: 2827
- Joined: 21 Nov 2005 20:39
- Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%
Re: SC: verifying user input to be a number - HOW?
I'd expect just the opposite: generally all variables are strings, and handled like that.autocart wrote:Then, in a comparison of "real" number against non-numeric string, is the "real" number converted into a string? Obiously yes, or am I wrong?
For a compare both are tested: do they just represent numbers? If yes for both they are converted and compared numerically. Else string-compare is done.
(I hope it's just as I say...
XYplorer Beta Club