Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: searching backwards in a string

by plaid (Chaplain)
on Jun 13, 2000 at 03:36 UTC ( #17812=note: print w/replies, xml ) Need Help??


in reply to searching backwards in a string

You might want to take a look at one of the CPAN modules to do text wrapping, namely Text::Wrapper, Text::Wrap, or Text::Format.

For a regular expression way to do it, how about:

while($text =~ /(.{0,80})\s+/g) { # result in $1 }

Replies are listed 'Best First'.
RE: Re: searching backwards in a string
by Anonymous Monk on Jun 13, 2000 at 16:06 UTC
    >> while($text =~ /(.{0,80})\s+/g) { # result in $1 } << I believe that the above snippet is an infinite loop. However, if TEXT is a filehandle then while(<TEXT> =~ /(.{0,80})\s+/g){ # result in $1 } should kill all trailing space and line returns. Isn't that what chomp is for, though?

      If all your code did was that very small snippet then it would indeed be an infinite loop. However, the loop was given as an example and the comment was supposed to be replaced with code that does something useful

      the inside of the loop should look something like: (untested)

      push @lines, substr $_, 0, length $1, "";

      Nuance

      The code snippit does not loop infinitely, regardless of loop body. As stated, the result is in $1. It then becomes trivial to write a test program to see how it works. In fact, I'll take care of it for you.
      use strict; my $text = "This is a long line that I am typing to demonstrate". " the way that my regular expression works and it's a". " run-on sentence too but I don't really care."; print $1, "\n" while($text =~ /(.{0,20})\s+/g);
      Has a slight problem of not grabbing the last line, but it's 99% of the way there, and besides, that's why you should use a module in the first place:)
RE: Re: searching backwards in a string
by Anonymous Monk on Jun 14, 2000 at 03:36 UTC
    >> while($text =~ /(.{0,80})\s+/g) { # result in $1 } << I believe that the above snippet is an infinite loop. However, if TEXT is a filehandle then while(<TEXT> =~ /(.{0,80})\s+/g){ # result in $1 } should kill all trailing space and line returns. Isn't that what chomp is for, though?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://17812]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2020-06-02 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (19 votes). Check out past polls.

    Notices?