Padding lines with spaces?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Padding lines with spaces?
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
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
-
- Livecode Opensource Backer
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).
Note that for adding spaces, you can use this function
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
Code: Select all
put format("%60s", "") into tSpaces
-- where 60 is the number of spaces to create
-
- Livecode Opensource Backer
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
-
- VIP Livecode Opensource Backer
- Posts: 7266
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Padding lines with spaces?
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.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
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
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
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
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
-
- Livecode Opensource Backer
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
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
-
- Livecode Opensource Backer