empty lines in field

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Preston Shea
Posts: 73
Joined: Sat Apr 08, 2006 6:51 pm

empty lines in field

Post by Preston Shea » Fri Nov 12, 2010 2:36 pm

When a field is sorted the empty lines are at the top. How can I delete these empty lines?
In nova fert animus mutatas dicere formas corpora.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: empty lines in field

Post by jmburnod » Fri Nov 12, 2010 3:02 pm

Hi Preston
Try this :

Code: Select all

on DoNoLineEmptyInFld 
   put "myField" into LeCurFld
   put NoLineEmpty(LeCurFld) into fld LeCurFld
end DoNoLineEmptyInFld

function NoLineEmpty pFld
   put fld pFld into rNoLineEmpty
   put the num of lines of rNoLineEmpty into nbl
   repeat with i = nbl down to 1
      if line i of rNoLineEmpty = empty then
         delete line i  of rNoLineEmpty
         wait 2 milliseconds 
      end if
   end repeat
   return rNoLineEmpty
end NoLineEmpty
All the best

Jean-Marc
https://alternatic.ch

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: empty lines in field

Post by Regulae » Fri Nov 12, 2010 4:37 pm

Hi Preston,

Another somewhat cruder way of deleting empty lines from the top of the field would be:

Code: Select all

on mouseUp
   repeat for 100
      if line 1 of fld "myField" is empty then
         delete line 1 of fld "myField"
      else 
         exit repeat
      end if
   end repeat
end mouseUp
This will delete up to 100 blank lines at the start of a field. You could of course increase the number, as it exits once it arrives at the first non-empty line. I mention it as a matter of interest, and also as a way of highlighting the advantages of Jean-Marc’s approach. It is more general than mine, because it will delete all blank lines occurring anywhere in a field (having first, for speed, put the contents of the field into the variable rNoLineEmpty). Note also how it counts down from the total number of lines in the field- this is important when you are deleting lines, as when you delete a line, all the lines which follow the deleted line are renumbered. Jean-Marc’s NoLineEmpty function is thus a handy general-purpose tool.
Edit: Another method which only removes blank lines at the top of a field is:

Code: Select all

on mouseUp
   put word 1 of fld "myField" into firstWord
   delete line 1 to (lineOffset(firstWord,fld "myField") - 1) in fld "myField"
end mouseUp
Regards,

Michael

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

Re: empty lines in field

Post by Klaus » Fri Nov 12, 2010 6:57 pm

Hi all,

the fastest way is and will always be a "repeat for each...." loop!

Code: Select all

...
put fld "with empty lines" into tField
lock screen
repeat for each line tLine in tField

  ## "repeat for each..." is READ only, so we cannot modify the content of tLine!
  ## But creating a new text variable will do
  if tLine <> empty then
    put tLine & CR after tNew
  end if
end repeat
delete char -1 of tNew
put tNew into fld "with empty lines"
unlock screen
...
This insanely fast, even with >10000 lines! :D


Best

Klaus

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: empty lines in field

Post by shaosean » Sun Nov 14, 2010 7:33 am

Why not use

Code: Select all

filter fld "myField" without EMPTY

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: empty lines in field

Post by jmburnod » Sun Nov 14, 2010 10:31 am

zwietzkstx ! ! :o

Code: Select all

filter fld "myField" without EMPTY
work fine for me

Best

Jean-Marc
https://alternatic.ch

Post Reply