Checking a numeric variable

Discuss and share scripts and script files...
Post Reply
Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Checking a numeric variable

Post by Marco »

Hi all,
I need to check if a variable is entirely made of digits, i.e. if it is a number. After that, I want to know if it is positive. Basically, I want it to be a positive number.
The last part is easy, since

Code: Select all

"$foo" > "0"
is all that is needed.
But what about the first part? According to Help

Code: Select all

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 I thought that

Code: Select all

"$foo" > "0"
would have killed two birds with a stone, returning true for every positive number. That's not true instead, because if you run

Code: Select all

 $delay = "bar";
 msg ("$delay" > "0");
you end up with a true. What I'm doing wrong?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: Checking a numeric variable

Post by FluxTorpedoe »

Hi'
Well, if you're not too allergic to regexes: :P

Code: Select all

  $foo = regexreplace($foo, "\d", "");
  msg ($foo == "");
Though there are certainly much proper ways to know that... (but I'm lazy). :biggrin:

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: Checking a numeric variable

Post by FluxTorpedoe »

Simpler:

Code: Select all

 $delay = "bar";
 msg ($delay*$delay > "0");
:!: No quotes for the vars.

Edit: Doesn't work.
If a var starts with number(s), it dismisses the rest, e.g. 5a * 5a = 25

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: Checking a numeric variable

Post by Marco »

FluxTorpedoe wrote:Hi'
Well, if you're not too allergic to regexes: :P

Code: Select all

  $foo = regexreplace($foo, "\d", "");
  msg ($foo == "");
Though there are certainly much proper ways to know that... (but I'm lazy). :biggrin:
Regex is my second name :D And was my first approach for this problem. But I was looking for a more elegant solution...
FluxTorpedoe wrote:Simpler:

Code: Select all

 $delay = "bar";
 msg ($delay*$delay > "0");
:!: No quotes for the vars.
Aw, brutal yet working! Will probably resort to that... Meanwhile, thanks!

Edit:
D'oh, you're right...
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

FluxTorpedoe
Posts: 906
Joined: 05 Oct 2011 13:15

Re: Checking a numeric variable

Post by FluxTorpedoe »

Hi' Marco Regex :biggrin:
You probably already have your own solution, but I just couldn't let it go... :mrgreen:
So to redeem myself here's one that's still not elegant but at least is a one-pass:

Code: Select all

  $delay = "bar";
  msg (regexreplace($delay, "(.*[a-zA-Z].*)", "-1") > 0);
(ofc we could use \D instead of [a-zA-Z] if we were sure the var doesn't start with + and/or contains other signs)

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: Checking a numeric variable

Post by Marco »

Solved with regex!

Code: Select all

 $delay = "foo";
 if (regexreplace("$delay", "[^\+\-\d]") == "$delay" AND "$delay" > 0) {
  msg "$delay is a positive number!";
 } else {
  msg "$delay is... something else.";
 };
The first strips out whatever is not a plus, a minus, or a digit, and compares with the original variable. Only a true number (made of digits and sign only) leads TRUE. (for the braves against the signs ,"\D")
The second checks if the variable is greater than zero. Only a positive number or a string lead TRUE.
The intersection gives me only positive numbers -> I'm happy :D
Anyway, thanks FluxTorpedoe for the interest!
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

Post Reply