Page 1 of 2

how to check if a line contains a number

Posted: Thu Apr 20, 2017 4:21 am
by audreyt
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!

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 7:23 am
by AndyP
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.

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 7:30 am
by AxWald
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!

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 8:28 am
by AndyP
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"

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 12:30 pm
by Klaus
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

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 2:36 pm
by dunbarx
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

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 3:06 pm
by audreyt
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

Posted: Thu Apr 20, 2017 4:17 pm
by jacque
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

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 4:51 pm
by Thierry
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

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 4:54 pm
by [-hh]
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 ;-)

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 5:00 pm
by Thierry
[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

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 5:06 pm
by SparkOut
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.)

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 5:07 pm
by Thierry
SparkOut wrote:No, I am so lazy I didn't supply an alternative solution 8)
Emptiness...
I'm with you :)

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 5:11 pm
by [-hh]
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 ;-)

Re: how to check if a line contains a number

Posted: Thu Apr 20, 2017 5:17 pm
by SparkOut
Being this lazy takes a lot of work you know!