Deleting Empty Lines From a Container
Mac OS 9 Mac OS X Windows Linux

Sometimes you'll have a variable or field that contains unwanted blank lines. Now one way to handle that is with a repeat loop:
repeat with x = (the number of lines of tData) down to 1
  if line x of tData is empty then delete line x of tData
end repeat
However this breaks down when you have thousands of lines of text, since the repeat with construct gets slower and slower as the value of x increases.

Those that are concerned with performance know that repeat for each if a lot faster, but it requires additional lines of code, and it also effectively stores a copy of your original container in memory, minus the blank lines:

put "" into tNewData
repeat for each line tLine in tData
  if tLine is not empty then
    put tLine & CR after tNewData
  end if
end repeat
delete char -1 of tData  -- removes trailing CR
However you can also do this with a simple one-liner, which shows the power of the Revolution scripting language:
filter tData without empty
This not only removes the blank lines, but also works on the same variable, thus conserving memory.


Posted 3/1/2006 by Ken Ray