Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to split a string into two lines, intelligently

by kweise (Novice)
on Oct 09, 2008 at 21:54 UTC ( [id://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

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://716320]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found