splitting lines

Discussion in 'Perl Misc' started by Bob Smith, Sep 22, 2003.

  1. Bob Smith

    Bob Smith Guest

    hi
    how to split text into lines?
    I have in a db whitespace delimited words forming chapters in one field,
    and I want to nicely put it on the page with max 30 or so characters per
    paragraph, after that line breaks should occur,
    but I can't figure out the regexp for doing it,
    any help much appreciated. Thank you
    /G
    Bob Smith, Sep 22, 2003
    #1
    1. Advertising

  2. Bob Smith

    Bigus Guest

    "Bob Smith" <> wrote in message
    news:...
    > hi
    > how to split text into lines?
    > I have in a db whitespace delimited words forming chapters in one field,
    > and I want to nicely put it on the page with max 30 or so characters per
    > paragraph, after that line breaks should occur,


    I take it you mean 30 characters per line rather than paragraph?

    If so, try something like this:

    @lines = $dbtext =~ /(.{30}\S*)/g;
    foreach(@lines)
    {
    $_ =~ s/^\s+//;
    print "$_\n";
    }

    Bigus
    Bigus, Sep 22, 2003
    #2
    1. Advertising

  3. Bob Smith

    D Borland Guest

    "Bigus" <> wrote in message
    news:QbGbb.1792$...
    >
    > "Bob Smith" <> wrote in message
    > news:...
    > > hi
    > > how to split text into lines?
    > > I have in a db whitespace delimited words forming chapters in one field,
    > > and I want to nicely put it on the page with max 30 or so characters per
    > > paragraph, after that line breaks should occur,

    >
    > I take it you mean 30 characters per line rather than paragraph?
    >
    > If so, try something like this:
    >
    > @lines = $dbtext =~ /(.{30}\S*)/g;
    > foreach(@lines)
    > {
    > $_ =~ s/^\s+//;


    can't that be typed just as s/^\s+//; not $_ =~ s^\s+//;

    > print "$_\n";
    > }
    >
    > Bigus
    >
    >
    D Borland, Sep 22, 2003
    #3
  4. Bob Smith

    Trent Curry Guest

    D Borland wrote:
    > "Bigus" <> wrote in message
    > news:QbGbb.1792$...
    >>
    >> "Bob Smith" <> wrote in message
    >> news:...
    >>> hi
    >>> how to split text into lines?
    >>> I have in a db whitespace delimited words forming chapters in one
    >>> field, and I want to nicely put it on the page with max 30 or so
    >>> characters per paragraph, after that line breaks should occur,

    >>
    >> I take it you mean 30 characters per line rather than paragraph?
    >>
    >> If so, try something like this:
    >>
    >> @lines = $dbtext =~ /(.{30}\S*)/g;
    >> foreach(@lines)
    >> {
    >> $_ =~ s/^\s+//;

    >
    > can't that be typed just as s/^\s+//; not $_ =~ s^\s+//;


    Yes it can, since s/ (a substitution op) with no lhs implies using $_;

    --
    Trent Curry

    perl -e
    '($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1)!eg
    ;print(reverse("$s")."\n");'
    Trent Curry, Sep 22, 2003
    #4
  5. Bob Smith

    Eric Bohlman Guest

    Bob Smith <> wrote in news::

    > hi
    > how to split text into lines?
    > I have in a db whitespace delimited words forming chapters in one field,
    > and I want to nicely put it on the page with max 30 or so characters per
    > paragraph, after that line breaks should occur,
    > but I can't figure out the regexp for doing it,


    You're better off using Text::Format, or Text::Autoformat, or a related
    module for this rather than trying to create a regexp. The authors of
    those modules have already dealt with a number of "gotchas" that occur in
    text formatting.
    Eric Bohlman, Sep 22, 2003
    #5
  6. Bob Smith

    Bigus Guest

    "Trent Curry" <> wrote in message
    news:bknh7a$rtb$...
    > D Borland wrote:
    > > "Bigus" <> wrote in message
    > > news:QbGbb.1792$...
    > >>
    > >> "Bob Smith" <> wrote in message
    > >> news:...
    > >>> hi
    > >>> how to split text into lines?
    > >>> I have in a db whitespace delimited words forming chapters in one
    > >>> field, and I want to nicely put it on the page with max 30 or so
    > >>> characters per paragraph, after that line breaks should occur,
    > >>
    > >> I take it you mean 30 characters per line rather than paragraph?
    > >>
    > >> If so, try something like this:
    > >>
    > >> @lines = $dbtext =~ /(.{30}\S*)/g;
    > >> foreach(@lines)
    > >> {
    > >> $_ =~ s/^\s+//;

    > >
    > > can't that be typed just as s/^\s+//; not $_ =~ s^\s+//;

    >
    > Yes it can, since s/ (a substitution op) with no lhs implies using $_;


    That's interesting.. I never knew that!

    Bigus
    Bigus, Sep 23, 2003
    #6
  7. Bob Smith <> wrote in message news:<>...
    > hi
    > how to split text into lines?
    > I have in a db whitespace delimited words forming chapters in one field,
    > and I want to nicely put it on the page with max 30 or so characters per
    > paragraph, after that line breaks should occur,
    > but I can't figure out the regexp for doing it,
    > any help much appreciated. Thank you
    > /G


    $outputline =~ s/(.{1,30})/$1\n/gs;

    print "$outputline";
    Mala Ananthamurthy, Sep 23, 2003
    #7
  8. Bob Smith <> wrote in message news:<>...
    > hi
    > how to split text into lines?
    > I have in a db whitespace delimited words forming chapters in one field,
    > and I want to nicely put it on the page with max 30 or so characters per
    > paragraph, after that line breaks should occur,
    > but I can't figure out the regexp for doing it,
    > any help much appreciated. Thank you
    > /G


    #This one looks for space character
    $outputline =~ s/(.{1,30})\s+/$1\n/gs;

    print "$outputline";
    Mala Ananthamurthy, Sep 23, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mothra
    Replies:
    1
    Views:
    383
    vkeyboard
    Jan 12, 2005
  2. John Ericson
    Replies:
    0
    Views:
    421
    John Ericson
    Jul 19, 2003
  3. Mark
    Replies:
    0
    Views:
    436
  4. John Dibling
    Replies:
    0
    Views:
    407
    John Dibling
    Jul 19, 2003
  5. Nakamura

    splitting a long token across lines

    Nakamura, Apr 4, 2004, in forum: Python
    Replies:
    4
    Views:
    317
Loading...

Share This Page