Fastest way to check if var contains numbers

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
istech
Posts: 211
Joined: Thu Sep 19, 2013 10:08 am

Fastest way to check if var contains numbers

Post by istech » Mon May 16, 2016 7:13 pm

Hi all,

I was using some expressions in livecode and thought I could use this expression to solve a simple problem to check if there is a number in a var.

put "2,3,4" into t

if 1 or 2 or 3 is in t then --works but convoluted
answer "yes"
end if

put item 1 into x --works but is there a faster way to write it--looking for a line wonder. :D
if item 1 is a number then
answer "yes"
end if


if t contains a number then --does not work..but should? I know I am missing something simple.
answer "yes"
end if

SparkOut
Posts: 2946
Joined: Sun Sep 23, 2007 4:58 pm

Re: Fastest way to check if var contains numbers

Post by SparkOut » Mon May 16, 2016 7:28 pm

if item 1 of t is a number then...

Now, did you want to ensure that all items of t contain a number for the condition to return true?

If so you, I think you would have to loop through the items to apply the test to each.

istech
Posts: 211
Joined: Thu Sep 19, 2013 10:08 am

Re: Fastest way to check if var contains numbers

Post by istech » Mon May 16, 2016 10:21 pm

SparkOut wrote:
If so you, I think you would have to loop through the items to apply the test to each.
That's what I was thinking. Just thought there was a quicker way.

example -
if the var contained nof3s would be true.
if var contained nofs would be false

Just was thinking why the "contain" can not be used. Anyway rules are rules.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Fastest way to check if var contains numbers

Post by dunbarx » Tue May 17, 2016 1:44 am

Well, if you want to really waste a bit of spare time, and go out of the box, and use a klugey LC method, and might have a million items to worry about, then try this in a button script:

Code: Select all

on mouseUp
   repeat 1000000
      put "1," after temp
   end repeat
   try
      put "X" into the last item of temp
      if sum(temp) is a number then answer "Good Number"
   catch tError
     answer "Bad Number"
   end try
end mouseUp
Comment out the "put X" line after running this. You did ask for "fast" you know.

Craig Newman

cubist
Posts: 5
Joined: Tue May 17, 2016 1:42 pm

Re: Fastest way to check if var contains numbers

Post by cubist » Tue May 17, 2016 2:04 pm

Hmmm… regex should work…

Code: Select all

put "2,3,4" into t

filter t with "*[0-9]*" -- this command looks for any of the 10 digits. if there is at least one digit in t, the filter will let it thru
if t <> "" then -- yes, the variable t does contain at least one digit
answer "yes"
else -- no, the variable t doesn't contain any digits
answer "no"
end if
I added the "answer 'no'" branch of that IF statement cuz I like to be thorough. Hope my code snippet helps…

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Fastest way to check if var contains numbers

Post by dunbarx » Tue May 17, 2016 2:15 pm

Cubist.

That is the right way to do it.

Craig

istech
Posts: 211
Joined: Thu Sep 19, 2013 10:08 am

Re: Fastest way to check if var contains numbers

Post by istech » Tue May 17, 2016 4:40 pm

cubist wrote:Hmmm… regex should work…

Code: Select all

put "2,3,4" into t

filter t with "*[0-9]*" -- this command looks for any of the 10 digits. if there is at least one digit in t, the filter will let it thru
if t <> "" then -- yes, the variable t does contain at least one digit
answer "yes"
else -- no, the variable t doesn't contain any digits
answer "no"
end if
I added the "answer 'no'" branch of that IF statement cuz I like to be thorough. Hope my code snippet helps…

Looks good to me. I wonder which will be quicker to loop through or filter? I will run a test. I wonder if there is large amount of text which would be quicker. :D

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Fastest way to check if var contains numbers

Post by Thierry » Tue May 17, 2016 6:39 pm

istech wrote: I wonder which will be quicker to loop through or filter? I will run a test.
I wonder if there is large amount of text which would be quicker.
Hi,

For large amount of text, this one will be fast enough:

Code: Select all

  
put "a,b,c,d,4,e,f" into t
   
 if matchChunk( t, "\d") then   answer "Yes"
 else answer "No"

 
Your original list is not transformed, matchChunk(), or matchText(),
doesn't matter which one you choose in this case,
will only scan your datas.

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply