develooper Front page | perl.beginners | Postings from March 2002

RE: help me with a better backstepper?

Thread Previous | Thread Next
From:
Russ Foster
Date:
March 6, 2002 10:32
Subject:
RE: help me with a better backstepper?
Message ID:
B156077CCE3DD1119DEC00805F350E1B01415A13@natasha.swc.com
DISCLAIMER: This is UNTESTED ... It's just something to try...

$OrgLine = "adshe ms0e sad qweoic,m qwod x0 vndu qiudb siu .... ";

While length of $OrgLine > 70 {
	Match on =~ /(.{0,70})\s/
	Print $1
	Remove $1 from $OrgLine ;
}
Print remainder of $OrgLine;

So, the regex would return (again, this is untested) $1 with up to 70
characters that terminated with a white space.

Just a thought ... 

-rjf


> -----Original Message-----
> From: M z [mailto:kpslater777@yahoo.com] 
> Sent: Wednesday, March 06, 2002 10:58
> To: beginners@perl.org
> Subject: help me with a better backstepper?
> 
> 
> Hi
> 
> I've written a little program to analyze lines that
> are longer than 70 characters, and if so, break at
> position 70 (if it is whitespace), if not whitespace
> (i.e. is in the middle of a word) it steps back to the
> first whitespace and breaks there.
> 
> However, I think there is probably a better way to do
> this.  Can someone either suggest an alternate route
> or improve this snippet?
> 
> ps. if you do suggest something cryptic, please detail
> for me, as I am still new!!! thanks
> 
> while(<F>) {
> chomp;
>       if (length $_ < 70){
>       print F1 "$_\n";
>       }
>       
>       else {
> 
>                 split(//, $_);
>                 if ($_[69] eq " ") {
>                 s/^(.{69}) (.*)$/$1\n+ $2\n/;
>                 print F1;
>                 }
>                 elsif($_[69] ne " " && $_[68] eq " ")
> {
>                 @_ = ();
>                 split(//, $_);
>                 s/^(.{68}) (.*)$/$1\n+ $2\n/;
>                 print F1 $_;
>                 }
>                 elsif($_[69] ne " " && $_[68] ne " "&&
> $_[67] eq " ") {
>                 @_ = ();
>                 split(//, $_);
>                 s/^(.{67}) (.*)$/$1\n+ $2\n/;
>                 print F1 $_;
>                 }
> 
> .....
> 
> # this is the strategy to back up 30 spaces
> eventually,
> but I don't want to have 30 elsif statements, although
> this works, it seems that a more experienced perl
> programmer could write this with much less code...
> 
> __________________________________________________
> Do You Yahoo!?
> Try FREE Yahoo! Mail - the world's greatest free email! 
http://mail.yahoo.com/

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About