Padding lines with spaces?

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Garrett
Posts: 386
Joined: Sat Apr 08, 2006 8:15 am
Contact:

Padding lines with spaces?

Post by Garrett » Wed Apr 19, 2006 1:14 am

Is there an quick and easy way to pad lines with spaces to match the
longest line in the list?

I can picture in my mind how to do this using two repeat loops, first to
find the longest line, and second to add the spaces to all the lines that
don't equal the length of the longest line, but I am hoping there is a
easier or faster way to do this.

Thanks,
-Garrett

marielle
Livecode Opensource Backer
Livecode Opensource Backer

Post by marielle » Wed Apr 19, 2006 2:05 am

Solution 1
2 repeat loops, but the second looping only on a portion of the text

In the first repeat, you check whether the current line is longer than the next one. If yes, you update a maxLength variable. If no, you fill with spaces (maxLength - length(line)) and store the value of the current line into tLineBeforeLongest.

For the second repeat loop, you repeat from 1 to the tLineBeforeLongest. Any line after tLineBeforeLongest has already been given correct spacing. If you are lucky, the tLineBeforeLongest is quite early in the file. If you are unlicky, it's quite late and the algorithm may happen to be slightly slower than a first loop to get the max length and a second to do fill with spaces.

Solution 2
Probing for the longest line using regular expressions.

Here I am using the {} syntax of regular expressions, where b{4} will match any part of the string that is bbbb (4 bs). But as getmatch takes longer than length(tLine), this is of interest only if (a) the text is very long and (b) the longest line is not that long and (c) you have a good bet on what the length of the longest line is. If you don't have such a bet, you should first use a {x,y} syntax, where x is the minimum number of occurences and y the maximum one. To speed up the script, [\n\r] should be replaced by what is relevant on your platform (see the entry on CR in the doc).

Code: Select all

put findLongestLine(tText, 200) into tMaxLength

function findLongestLine pText, pGuessOnMax
  repeat with x = pGuessOnMax down to 1
       get matchtext(cr & pText & cr, "[\n\r]([^\n\r]{" & x & "}[\n\r])", tMatch)
       if it is true then exit repeat
  end repeat
  return x
end findLongestLine
Note that for adding spaces, you can use this function

Code: Select all

put format("%60s", "") into tSpaces 
  -- where 60 is the number of spaces to create

Garrett
Posts: 386
Joined: Sat Apr 08, 2006 8:15 am
Contact:

Post by Garrett » Wed Apr 19, 2006 2:34 am

Hi Marielle,

Thanks for the reply. This is basically what I had in mind first, but was
hoping for something better. But looks like this maybe the only solution,
I'm going to go with it. :-)

Thanks,
-Garrett

marielle
Livecode Opensource Backer
Livecode Opensource Backer

Post by marielle » Wed Apr 19, 2006 9:45 am

Note that if your text is within a textfield, you can come up with a pretty close approximation with the code here. Don't forget to set a large, oversized, monospace font for the textfield before running the script (the larger the textfont, the better the estimation).

Code: Select all

answer findLongestLine2(the long name of field 1)
function findLongestLine2 pField
  put the formattedwidth of char 1 of line 1 of pField into tCharWidth
  put the formattedwidth of pField into tTextWidth
  return (tTextWidth / tCharWidth )
end findLongestLine2

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7257
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Padding lines with spaces?

Post by jacque » Thu Apr 20, 2006 12:49 am

Garrett wrote:Is there an quick and easy way to pad lines with spaces to match the
longest line in the list?

I can picture in my mind how to do this using two repeat loops, first to
find the longest line, and second to add the spaces to all the lines that
don't equal the length of the longest line, but I am hoping there is a
easier or faster way to do this.

Thanks,
-Garrett
I'd loop once to find the length of the longest line, then run the function below to do the padding. A trick is to pre-define a constant or variable that contains more spaces than you will ever need. It is much faster to grab a chunk of characters out of a string of spaces than to loop adding spaces individually. It is also faster to build a new list than to add to an old one.

So I'd:

Code: Select all

 on fillField tFld pWidth -- width previously determined
	get fld tFld
	repeat for each line L in it
		put leftAlign(L,pWidth) & cr after tNewList
	end repeat
	put tNewList into fld tFld
 end fillField
 
 
 constant kSpaces="                                                "

function leftAlign pText,pWidth
  put char 1 to pWidth of pText into pText -- trucate any extra chars 
  put char 1 to (pWidth-length of pText) of kSpaces after pText -- or "before" for right-aligned
  return pText
 end leftAlign
Use enough spaces between the quotes to cover the column width, for worst cases.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Timothy
Posts: 78
Joined: Tue Apr 11, 2006 7:38 pm

Post by Timothy » Thu Apr 20, 2006 7:53 pm

On a related note...

I recently tried to tidy up some clickable index fields by adding multiple spaces after the next-to-last word or item in a non-wrap field. The idea was that the final word or item, representing the desired destination when I clicked on the line, would not be visible. The series of spaces would push this item or word past the right margin of the field.

As far as I could tell, the IDE eliminated the long seqence of spaces. Presumably that's a feature. I couldn't figure out what to do about it, finally gave up.

Suggestions or comments?

Thanks,

Tim
It's better to be happy and rich than poor and sad -- W. C. Fields

marielle
Livecode Opensource Backer
Livecode Opensource Backer

Post by marielle » Thu Apr 20, 2006 10:06 pm

Timothy wrote:As far as I could tell, the IDE eliminated the long seqence of spaces. Presumably that's a feature. I couldn't figure out what to do about it, finally gave up.
Did you try adding a character other than a space or a tab at the end of each line, right after the trailing spaces?

Timothy
Posts: 78
Joined: Tue Apr 11, 2006 7:38 pm

Post by Timothy » Thu Apr 20, 2006 11:04 pm

it was some words or items, a couple of dozen spaces, then one more word or item.

Maybe I was doing something stupid, but I messed around with it for awhile, and the spaces kept disappearing from the field when the script ran.

I tried it with a sequence of tabs, too.

If this sounds odd, maybe I should try to replicate it again. It's been awhile.

Thanks Marielle,

Tim
It's better to be happy and rich than poor and sad -- W. C. Fields

marielle
Livecode Opensource Backer
Livecode Opensource Backer

Post by marielle » Fri Apr 21, 2006 2:15 am

Timothy wrote:it was some words or items, a couple of dozen spaces, then one more word or item.
Sorry, I didn't read carefully enough. Why not paste your code so far?

Timothy
Posts: 78
Joined: Tue Apr 11, 2006 7:38 pm

Post by Timothy » Fri Apr 21, 2006 7:24 pm

Sorry, I haven't worked on this problem for awhile. I probably trashed the problem code. I'll try some test scripts soon. If the same problem arises, I'll start a new thread.

Tim
It's better to be happy and rich than poor and sad -- W. C. Fields

Post Reply

Return to “Talking LiveCode”