Mjpaddy:

You've already received some nice answers, so you won't want to use this ridiculous one. I just created it to amuse myself. I'm only posting it because: (a) It amused me to do so, and (b) 'cause I appropriated 2teez's signature line as part of the example, who might also be amused... ;^D

sub splitit { my ($left, @words, $right, $doit) = split /\s+/, shift; $right = pop @words; $doit = sub { my ($left, $right, @rest) = @_; if (@rest) { if (length($left) < length($right)) { return $doit->($left . ' ' . shift @rest, $right, @res +t); } else { return $doit->($left, pop(@rest) . ' ' . $right, @rest +); } } return $left, $right; }; $doit->( $left, $right, @words); } while (<DATA>) { s/\s+$//; print join(" | ", splitit($_)), "\n" unless /^$/;; } __DATA__ Now is the time for all good men to come to the aid of their party. The quick red fox jumped over the lazy brown dog. Time flies like an arrow, fruit flies like a banana. If you tell me, I'll forget. If you show me, I'll remember. If you involve me, I'm calling the cops.

Note: This code is an example of *bad* programming style, so don't use it, and it uses recursion instead of a simple loop which would be simpler and more efficient.

Update: A better version, which is still useless:

#!/usr/bin/env perl # # split a sentence into two roughly equal parts # use strict; use warnings; sub splitit { my $sentence = shift; my @words = split /(\s+)/, $sentence; my $doit; $doit = sub { my ($left, $right, @rest) = @_; if (@rest) { if (length($left) < length($right)) { return $doit->($left . shift @rest, $right, @rest); } else { return $doit->($left, pop(@rest) . $right, @rest); } } return $left, $right; }; return $doit->( '', '', @words); }

Finally, using a loop:

sub splitit { my @words = split /(\s+)/, shift; my ($left, $right) = ('', ''); while (@words) { if (length($left) < length($right)) { $left .= shift @words; } else { $right = pop(@words) . $right; } } return $left, $right; }

...roboticus

If ya wanna get to be good at anything, keep doing it. If ya wanna keep doing it, play with it and make it fun.


In reply to Re: Break sentence into two part by roboticus
in thread Break sentence into two part by Mjpaddy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.