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

help me with a better backstepper?

Thread Next
From:
M z
Date:
March 6, 2002 08:58
Subject:
help me with a better backstepper?
Message ID:
20020306165820.57052.qmail@web20908.mail.yahoo.com
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/

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