http://www.perlmonks.org?node_id=716320

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

Hi all, I need to split a user entered string into two lines, the first containing 50 or less characters, the second the remainder. I want to split the string on a space, so that the two lines don't split in the middle of a word.
As a shorter example "The quick brown fox jumped over the lazy dog" into:
The quick brown fox jumped
over the lazy dog

Not breaking the string in the middle of a word. But the string will be different each time, depending on what the user entered. Thanks for any help!!
  • Comment on How to split a string into two lines, intelligently

Replies are listed 'Best First'.
Re: How to split a string into two lines, intelligently
by almut (Canon) on Oct 09, 2008 at 22:01 UTC

    If you just want to get it working (and are not specifically interested in the learning experience per se), you might want to consider using Text::Wrap :)

Re: How to split a string into two lines, intelligently
by ig (Vicar) on Oct 09, 2008 at 22:44 UTC
    To get a little closer to your objective, you might try one of the following:
    #!/usr/bin/perl -w # use strict; my $string = "the quick brown fox jumped over the lazy dog"; # Using a regular expression $string =~ m/(.{0,50}\s)(.*)/; print "$1\n$2\n"; # Using rindex() and substr() my $pos = rindex($string, ' ', 50); print substr($string,0,$pos) . "\n" . substr($string,$pos+1) . "\n";
    The regular expression will break the line on any whitespace (space, tab, etc.) but rindex will break only on a space character.
      Thanks! The regex worked great, that's still my weak point, even when working in unix/linux.
Re: How to split a string into two lines, intelligently
by RMGir (Prior) on Oct 09, 2008 at 22:46 UTC
    "Holy homework, Batman!"

    What you're looking for is a way to search a string and replace, from the start of a string, a space that's as many as 50 characters in with a newline.

    Now, if only perl had a mechanism to make this simple. And if only this mechanism had something called "quantifiers" you could use to specify your limit! Wouldn't that be nice?

    Please, click on the link above and read the perlre manpage. I'm going to post an answer behind spoiler space, but you'll do better in the long run if you read and understand the fine manual.


    Mike
      Not homework, just a perl beginner, trying to take user input from our company home page and get it printed nicely. At least I figured out how to use PDF::API2 myself. ;-)
Re: How to split a string into two lines, intelligently
by GrandFather (Saint) on Oct 09, 2008 at 23:00 UTC

    My first PerlMonks node provided a solution to a problem very like this one. If you twiddle the values in the quantifier and remove the /g switch then you have the solution you are probably looking for.

    See perlretut, perlre and perlreref for more regular expression goodness.


    Perl reduces RSI - it saves typing
Re: How to split a string into two lines, intelligently
by Andrew Coolman (Hermit) on Oct 09, 2008 at 22:41 UTC
    This one might work if the characters are separated by spaces:
    @arr = split (/\s/,$string); $part1 = (); $part2 = (); $len = 0; while (($len += 1 + length($part = shift @arr)) < 50) { $part1 .= "$part "; } $part2 = join(' ', @arr);

    Regards,
    s++ą  ł˝ ął. Ş ş şą Żľ ľą˛ş ą ŻĽąş.}++y~-~?-{~/s**$_*ee
Re: How to split a string into two lines, intelligently
by JavaFan (Canon) on Oct 09, 2008 at 22:15 UTC
    Assuming the first word doesn't consist of more than 50 characters (in which case, no solution is possible), a simple
    s/\s/\n/;
    will do. The first line will not be more than 50 characters, and the remainder will be in te second line.

    It's not the solution that makes the longest first string possible with the given constraints, but then, you weren't asking for it.