Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

How to split a string into two lines, intelligently

by kweise (Novice)
on Oct 09, 2008 at 21:54 UTC ( #716320=perlquestion: print w/replies, xml ) Need Help??
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.

    $text=~s{^ # starting at the beginning of the string (.{1,50}) # capture up to 50 characters \s # followed by a space } {$1\n}x; # and replace it with what you captured followed +by a newline

      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 (Sage) 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);

    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
    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://716320]
Approved by almut
[ambrus]: choroba: that doesn't matter, the cookies are independent on the webserver, they just have to contain your username and crypted password with a seed of the user's choice, you can change the hostname or even construct a cookie without asking the server
[ambrus]: choroba: and for perlmonks (but not for everything2), you don't even need a cookie, you can just send a username and password parameter in every request, and this is even documented in What XML generators are currently available on PerlMonks?
[holli]: i admire you choroba, if i had to work at such a place, i wouldn't last long. as bosses don't like if someone calls them clueless idiots
[ambrus]: (The cookie format is not documented anywhere afaik, but it's trivial to reverse engineer even without being a pmdev.)
[holli]: or "ignorant bricks" (that is not a typo)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2017-10-24 11:31 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (289 votes). Check out past polls.