how to check if a line contains a number
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
how to check if a line contains a number
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!
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!
Re: how to check if a line contains a number
Try this
contains "012345" in your example will try to match the whole string within quotes.
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!
Re: how to check if a line contains a number
Hi,
there's some slight mistakes in your code. Try this:
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:
Pondering why this works better (and why it's nearly 10 times faster) is left as an exercise ;-)
Have fun!
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
"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"
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!
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!
Re: how to check if a line contains a number
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.
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!
Re: how to check if a line contains a number
Hi audreyt,
1. welcome to the forum!
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.
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
...
Best
Klaus
1. welcome to the forum!

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.

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
...

Best
Klaus
Re: how to check if a line contains a number
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:
This goes through a lot of chars, but is pretty fast. Anyway, you could:
Just another way to think about things...
Craig Newman
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
Code: Select all
repeat for each line tLine in yourColorList
if goodnumber(tLine) = "" then put tLine & return after numberlessColors
end repeat
Craig Newman
Re: how to check if a line contains a number
Thank you all so much for your help! I really appreciate it. I used AxWald's code and it worked great.
Re: how to check if a line contains a number
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
HyperActive Software | http://www.hyperactivesw.com
Re: how to check if a line contains a number
or with the same logic but less lines for lazy people like me: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
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.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: how to check if a line contains a number
Yet another solution.
Requires LC version >= 6.1 and a super-lazy Thierry 
Code: Select all
on mouseUp
filter the colorNames without regex pattern "[0-9]+" into fld 1
end mouseUp

shiftLock happens
Re: how to check if a line contains a number
[quote="[-hh]"]Yet another solution.
You're the King of lazy-land 
Regards,
Thierry
Code: Select all
on mouseUp
filter the colorNames without regex pattern "[0-9]+" into fld 1
end mouseUp

Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: how to check if a line contains a number
No, I am so lazy I didn't supply an alternative solution 
(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.)

(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.
Re: how to check if a line contains a number
Emptiness...SparkOut wrote:No, I am so lazy I didn't supply an alternative solution
I'm with you

!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: how to check if a line contains a number
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).
But of course SparkOut beats everybody with his lazyness -- except when MariaSole is asking 
Code: Select all
on mouseUp
filter colorNames() without regex pattern "[0-9]" into fld 1
end mouseUp

shiftLock happens
Re: how to check if a line contains a number
Being this lazy takes a lot of work you know!