how to check if a line contains a number

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

audreyt
Posts: 2
Joined: Thu Apr 20, 2017 4:14 am

how to check if a line contains a number

Post by audreyt » Thu Apr 20, 2017 4:21 am

Hello!
I am trying to sort through a list of information and delete any line that contains a number (several lines contain a to z characters as well as numbers)
I've tried using an isNumber function, and several variations of the code below. I feel like I've gotten close a few times, but am messing up the syntax. The error typically comes on line 4. Any ideas?

on preOpenStack
put the colorNames into colorVar
repeat for each line colorLine in colorVar
if line colorLine of colorVar contains "012345"
then
delete the line colorLine in colorVar
else
put colorLine & cr after fld "colorNames"
end if
end repeat
end preOpenStack

The error I get is: execution error at line 804 (Handler: can't find handler) near "revSEObjectDeleted", char 1

Thanks!

AndyP
Posts: 634
Joined: Wed Aug 27, 2008 12:57 pm
Contact:

Re: how to check if a line contains a number

Post by AndyP » Thu Apr 20, 2017 7:23 am

Try this

Code: Select all

put the colorNames into colorVar
put replaceText(colorVar,"([0-9])","") into fld "colorNames"

contains "012345" in your example will try to match the whole string within quotes.
Andy .... LC CLASSIC ROCKS!

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: how to check if a line contains a number

Post by AxWald » Thu Apr 20, 2017 7:30 am

Hi,

there's some slight mistakes in your code. Try this:

Code: Select all

   repeat for each line colorLine in colorVar
      if colorLine contains "012345" then
         -- delete the line colorLine in colorVar  -- <== would this ever run, it would crash!
      else
         put colorLine & cr after fld "colorNames"
      end if
   end repeat
See the difference?
"colorLine" is a variable containing a line of colorVar, so no "of colorVar ".
Same in the line that would run would colorLine actually contain "012345". But it's not needed anyways, so I quickly wrote a working version:

Code: Select all

   put the colorNames into colorVar
   repeat for each line colorLine in colorVar
      put false into myLineHasNumbers
      repeat for each char ColChar in colorLine
         if ColChar is a number then
            put true into myLineHasNumbers
            exit repeat
         end if
      end repeat
      if not myLineHasNumbers then put colorLine & cr after myVar
   end repeat
   put myVar into fld "colorNames"
Pondering why this works better (and why it's nearly 10 times faster) is left as an exercise ;-)

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

AndyP
Posts: 634
Joined: Wed Aug 27, 2008 12:57 pm
Contact:

Re: how to check if a line contains a number

Post by AndyP » Thu Apr 20, 2017 8:28 am

Ah, just reread the post.

My original post just strips out numbers.

This will do what you want and simple to follow, using two repeat loops. One for the line and one for the char to check against.

Code: Select all

   put the colorNames into colorVar
   repeat with colorLine = 1 to the number of lines in colorVar
      repeat with tCount = 1 to 5
         if line colorLine of colorVar contains tCount then
            delete line colorLine in colorVar
         end if
      end repeat
   end repeat
   put colorVar into fld "colorNames"
Andy .... LC CLASSIC ROCKS!

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: how to check if a line contains a number

Post by Klaus » Thu Apr 20, 2017 12:30 pm

Hi audreyt,

1. welcome to the forum! :D

2. As others already pointed out, when you "repeat for each line XYZ..." XYZ will contain the CONTENT of that line and not the line number.
AND, most important, the loop is read only!
Means you cannot modify or delete XYZ, but it is lightning fast. :D

3. To all:
Be careful if you "repeat with x = 1 to the num of lines of xyz" and DELETE a line in the loop.
in that case "the number of lines" will naturally change and LC will throw an error.

To avoid this inconvenience, start at the top::
...
repeat with x = the number of lines of XYZ down to 1
...
8)


Best

Klaus

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

Re: how to check if a line contains a number

Post by dunbarx » Thu Apr 20, 2017 2:36 pm

About 25 years ago in HC, I wrote a general purpose function that I still use all the time. It strips all non-numeric chars from a string. The LC version is:

Code: Select all

function goodNumber var
   repeat for each char tChar in var
      if tChar is in "0123456789." then put tChar after temp
   end repeat
   return temp
end goodNumber
This goes through a lot of chars, but is pretty fast. Anyway, you could:

Code: Select all

repeat for each line tLine in yourColorList
  if goodnumber(tLine) = "" then put tLine & return after numberlessColors
end repeat
Just another way to think about things...

Craig Newman

audreyt
Posts: 2
Joined: Thu Apr 20, 2017 4:14 am

Re: how to check if a line contains a number

Post by audreyt » Thu Apr 20, 2017 3:06 pm

Thank you all so much for your help! I really appreciate it. I used AxWald's code and it worked great.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: how to check if a line contains a number

Post by jacque » Thu Apr 20, 2017 4:17 pm

Just for future reference, I might do something like this:

Code: Select all

put the colornames into colorVar
  repeat for each line colorLine in colorVar
    if not matchtext(colorLine,"[0-9]") then
      put colorLine & cr after tList
    end if
  end repeat
put tList into fld 1
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: how to check if a line contains a number

Post by Thierry » Thu Apr 20, 2017 4:51 pm

jacque wrote:

Code: Select all

put the colornames into colorVar
  repeat for each line colorLine in colorVar
    if not matchtext(colorLine,"[0-9]") then
      put colorLine & cr after tList
    end if
  end repeat
put tList into fld 1
or with the same logic but less lines for lazy people like me:

Code: Select all

   put the colornames into colorVar
   filter colorVar without regex pattern "[0-9]"
   put colorVar into fld 1
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: how to check if a line contains a number

Post by [-hh] » Thu Apr 20, 2017 4:54 pm

Yet another solution.

Code: Select all

on mouseUp
  filter the colorNames without regex pattern "[0-9]+" into fld 1
end mouseUp
Requires LC version >= 6.1 and a super-lazy Thierry ;-)
shiftLock happens

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

Re: how to check if a line contains a number

Post by Thierry » Thu Apr 20, 2017 5:00 pm

[quote="[-hh]"]Yet another solution.

Code: Select all

on mouseUp
  filter the colorNames without regex pattern "[0-9]+" into fld 1
end mouseUp
You're the King of lazy-land :)

Regards,

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

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

Re: how to check if a line contains a number

Post by SparkOut » Thu Apr 20, 2017 5:06 pm

No, I am so lazy I didn't supply an alternative solution 8)

(although I would have spent more time on explaining the difference between repeat for each and repeat with index, and how for each is faster, also how deleting an indexed line requires to start at the highest index and count down. But I was lazy and Klausimausi outklaussed me again.)
Last edited by SparkOut on Thu Apr 20, 2017 5:08 pm, edited 1 time in total.

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

Re: how to check if a line contains a number

Post by Thierry » Thu Apr 20, 2017 5:07 pm

SparkOut wrote:No, I am so lazy I didn't supply an alternative solution 8)
Emptiness...
I'm with you :)
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: how to check if a line contains a number

Post by [-hh] » Thu Apr 20, 2017 5:11 pm

Thierry you are right, the plus in "[0-9]+" is superfluous. And "the colornames" may be replaced by "colornames()". So the following may be really minimal (3 chars less than my first post).

Code: Select all

on mouseUp
  filter colorNames() without regex pattern "[0-9]" into fld 1
end mouseUp
But of course SparkOut beats everybody with his lazyness -- except when MariaSole is asking ;-)
shiftLock happens

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

Re: how to check if a line contains a number

Post by SparkOut » Thu Apr 20, 2017 5:17 pm

Being this lazy takes a lot of work you know!

Post Reply