Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How to split a string into two lines, intelligently

by ig (Vicar)
on Oct 09, 2008 at 22:44 UTC ( #716332=note: print w/ replies, xml ) Need Help??


in reply to How to split a string into two lines, intelligently

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.


Comment on Re: How to split a string into two lines, intelligently
Download Code
Re^2: How to split a string into two lines, intelligently
by kweise (Novice) on Oct 10, 2008 at 15:54 UTC
    Thanks! The regex worked great, that's still my weak point, even when working in unix/linux.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://716332]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2015-07-03 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls