LPM: filtering

Mik Firestone fireston at lexmark.com
Tue Mar 7 15:26:10 CST 2000


Sigh.  I never showed you this.  Please understand it actually only splits the
line at the whitespace nearest to char 50.  It will not slice a line into as
many ~50 char pieces as it can.  As usual, this is very specific and runs
fast.  Text::Wrap does it generically and runs slower.  Your choice.

To see how this works, pipe ypcat across <STDIN>.

The regex is, of course, the heart and soul.  You can change the record
seperator by replacing the space.  You can control the length of the bits by
changing the 50.  It can go fast or work well.  Which one do you want?

With neat tricks, you could probably come up with a more generic solution:
    while ( length( $string ) > 50 ) {
	$string =~ s/^(.{0,50} (.+)$/$2/;
	push @stuff, $1;
    }
That is untested.  Try it - it may actually work.

Mik
-------- Included code --------

#!/os/dist/bin/perl -w

$" = "|";
while ( <STDIN> ) {
    @resp = ( /(.{0,50}) (.+)/g );
    printf "I split line %d into %d pieces\n", $., scalar @resp;
    print "@resp\n";
}

-- 
Mik Firestone fireston at lexmark.com
When I become an Evil Overlord:
I will have young lads/lasses in strange clothes, with foreign accents,
REGULARLY climb some monument in the main sqaure of my capital and denounce
me, claim to know the secret of my power, rally the masses to rebellion, etc.
That way, the citizens will be jaded in case the real thing ever comes along.

On Tue, 7 Mar 2000, Frank Price wrote:

> On Tue, 7 Mar 2000, Joe Hourcle wrote:
> 
> # On Tue, 7 Mar 2000, Frank Price wrote:
> # 
> # > Hi LexPM,
> # > 
> # > I want to text-wrap a string to 50 columns before printing it to
> # > sendmail.  In the shell, I'd do this:  
> # > 
> # >   echo "Really long string ..." | fmt -50 | mail 
> # > 
> # > How can I do the same in perl?  I tried something like the following,
> # > essentially from the Cookbook, but it just printed to my terminal's
> # > stdout and then hung until I ctrl-D.  I realize I could use a temp
> # > array to hold the formatted output, but for some reason this seems
> # > more "elegant" -- is there any way to make it work?
> # 
> # I've never used it, but both the format command, and Text::Wrap chould do
> # it..
> # 
> # using Text::Wrap, it'd be something like this:
> 
> Yah, I know about Text::Wrap, and it works great.  Seemed a shame to
> load a module just for that if I could roll my own though (although
> forking might be just as bad).  
> 
> Anyway, a more abstract version of my question is:  Can I pipe to a
> program, print to it, and then read from that stdout so I can do
> something else with it?  The "fork off another process" idea from the
> Cookbook seems to be what I want, but something's going wrong.
> 
> -Frank.
> --
> Frank Price											fprice at mis.net
> E Pluribus Unix | Linux: Choice of a GNU Generation | Why not go mad?
> 
> 
> 




More information about the Lexington-pm mailing list