holly has asked for the wisdom of the Perl Monks concerning the following question:

I'm new to perl so this is probably going to end up being a simple and stupid problem, but I was just trying to write a script that would take a text file and add newlines after 80 chars or the end of the nearest word. Here's what I've got, but it ain't workin':

foreach $line (@file) { $line =~ s/\s\b{70}/$1\n/g); }

If anyone has any advice, I'd love to hear it!

thanks,
Holly

edit:replaced pre-tag with code-tag (neophyte, 2001-08-27)

  • Comment on How do I split a string at a char position that's also the end of a word?
  • Download Code

Replies are listed 'Best First'.
Re: How do I split a string at a char position that's also the end of a word?
by japhy (Canon) on Aug 27, 2001 at 15:17 UTC
    The problem is that \b matches a location, and not actual text. I have a feeling you'll want to write something like:
    s/(.{0,80})\b/$1\n/g for @file;
    This will match up to 80 (non-newline) characters, and try to match a word boundary after them. If it can't, it'll back up one character and try again.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re (tilly) 1: How do I split a string at a char position that's also the end of a word?
by tilly (Archbishop) on Aug 27, 2001 at 16:07 UTC
    When you have written your script, I would suggest rewriting it using the CPAN module Text::Autoformat.

    The experience should show you why it is good to use CPAN when you can. :-)

Re: How do I split a string at a char position that's also the end of a word?
by MZSanford (Curate) on Aug 27, 2001 at 15:13 UTC
    hmmm, wrapping text ... i would think (untested):
    foreach my $line (@file) { $line =~ s/(.{70}\B{1,10})/$1\n/g); }
    you are not working because of a few things. the ones which stick out are the missing parens in the first half of the s///, which define $1. The other is the \b{70}, which looks for 70 word boudries in a row. While my solution may not work, these are a few things that might help you find the way. My version gobbles 70 chars, then starts looksing for boundries over the next 10 chars. A better way would be to read 80 chars, then back track to the last word boudry, or, see Text::Wrapper
    can't sleep clowns will eat me
    -- MZSanford
Re: How do I split a string at a char position that's also the end of a word?
by htoug (Deacon) on Aug 27, 2001 at 16:28 UTC
    Use the Text::Wrap module, in the core for newer perls, or on CPAN.
Re: How do I split a string at a char position that's also the end of a word?
by lshatzer (Friar) on Aug 27, 2001 at 18:09 UTC