[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] string evenness



On Dec 1, Jeff Pinyan said:

>  $half = int(length($str)/2);
>  $str =~ s{( ?)([^ ]*)(?<=^.{$half})([^ ]*)( ?)}
>           { length($2) < length($3) ? "\n$2$3$4" : "$1$2$3\n" }e;

Generalized (I wish I could get it with one regex, but alas):

  $str = "of course, japhy thought of this one";
  $parts = 1 + int rand 5;
  $len = int(length($str)/$parts);

  while ($str =~ s{( ?)([^ ]*)(?<=^.{$len})([^ ]*)( ?)}
    { length($2) < length($3) ? "\n$2$3$4" : "$1$2$3\n" }e
  ) {
    push @parts, ($str =~ s/(.*\n?)// and $1);
    last if length $str <= $len;
  }

  $str = join("", @parts) . $str;

  print $str;

-- 
Jeff "japhy" Pinyan     japhy@pobox.com    http://www.pobox.com/~japhy/
CPAN - #1 Perl Resource  (my id:  PINYAN)       http://search.cpan.org/
PerlMonks - An Online Perl Community          http://www.perlmonks.com/
The Perl Archive - Articles, Forums, etc.   http://www.perlarchive.com/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe