[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] right-justified line wrapping



Yitzchak Scott-Thoennes wrote:

> Am I missing something ?

Nope, except for the backwards angle bracket RK pointed out.

> > perl -pe 's/(.{1,80})\s/$1\n/g'
> 
> I would make that (.{1,80}\S)\s+

The target corpus did not have any multiple spaces in it.

*****

Word processors in the eighties had "justify" features that
would right-justify text on fixed-width printers, by adding spaces.

Text::Wrap does not have a justify feature.  Looking at
CPAN, there is Text::WrapProp but that is concerned with proportional
typefaces, with their widths stored in a table.


I have seen web pages "formatted" as plain text with old-style
right-justification, as a joke.

Is there any trick to getting the spaces to even out?  I suppose
my loop from yesterday would work, with an extra step thrown in to
double spaces until the line is too long, and then remove random
spaces until the line is exactly WIDTH wide, but how to do this
within a single s///?

Starting always from the left it might work like this:


# right-justify to 80 char lines
chomp;
while(length < 80){

	s/\b \b/\b  \b/ and next;
	s/\b  \b/\b   \b/ and next;
	s/\b   \b/\b    \b/ and next;
	s/\b    \b/\b     \b/ and next;
	s/\b     \b/\b      \b/ and next;
};

Perhaps some analysis and mathematics would be appropriate?

chomp;

$SpacesToAdd = 80 - length;

@words = split /\s+/;

while ($SpacesToAdd--){

	$words[rand $#words] .= ' ';

};




-- 
                          David Nicol 816.235.1187 nicold@umkc.edu
                                       Visualize the cosmic muffin

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe