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

[FWP] new real world golf hole: line wrapping (proposed and solved)





Last night I was faced with the task of printing out Neal Stephenson's
essay "In the beginning there was the command line" ( archived at
http://suckfanclub.dhs.org/members/beefurabi/StephensonCommandLine.txt
) on a HP deskjet.  After discovering that none of the tools that came with
windows 98 or Caldera OpenLinux (which has a very nice automounter) could
perform a word wrap service and save the text file as a wrapped text file,
since I wanted to print the document onto paper in as little time as possible
and really didn't care if it was in Times New Roman or not (this printer
has a native mode in which it prints 80 character lines) I recalled
that I had installed ActiveState perl on it, so I wrote a short program
involving a while(<>) and a s/// and two gotos which, pleasingly, worked
the first time (thanks to there being no sequences that go 80 characters 
without a \s of some kind.)


# this is wrap80.pl
while(<>){

	TOOLONG:

	goto DONE if length <= 81;

	s/^(.{1,80})\s//;
	print "$1\n";

	goto TOOLONG;

	DONE:
	print;
};



I was then able to pipe the output of this to the printer device
(which turned out to be PRN but /dev/lp0 would have worked too,
although I don't know if caldera OpenLinux would have accurately
interpreted "out of paper" signals as well as win89 did)
to generate about sixty pages of perfectly readable "manuscript."


I know this could be improved, massively improved, in a variety
of ways.

mmmmmm in fact, does

perl -pe 's/(.{1,80})\s/$1\n/g'

do it?

Looks like it does.  Well defined greedy matching to the rescue!

(discourage me to prevent future postings such as this)

-- 
                          David Nicol 816.235.1187 nicold@umkc.edu
                                            Visualize creamed corn

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