Page 1 of 1
Fastest way to check if var contains numbers
Posted: Mon May 16, 2016 7:13 pm
by istech
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.
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
Re: Fastest way to check if var contains numbers
Posted: Mon May 16, 2016 7:28 pm
by SparkOut
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.
Re: Fastest way to check if var contains numbers
Posted: Mon May 16, 2016 10:21 pm
by istech
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.
Re: Fastest way to check if var contains numbers
Posted: Tue May 17, 2016 1:44 am
by dunbarx
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
Re: Fastest way to check if var contains numbers
Posted: Tue May 17, 2016 2:04 pm
by cubist
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…
Re: Fastest way to check if var contains numbers
Posted: Tue May 17, 2016 2:15 pm
by dunbarx
Cubist.
That is the right way to do it.
Craig
Re: Fastest way to check if var contains numbers
Posted: Tue May 17, 2016 4:40 pm
by istech
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.

Re: Fastest way to check if var contains numbers
Posted: Tue May 17, 2016 6:39 pm
by Thierry
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