in reply to spliting text!!

This could be a job for split!

use strict; use warnings; my $string = 'abcdef123456' x 20; my @array = split(/(.){70}/, $string); @array = grep { defined } @array;

Note: Code is not tested

emc

Insisting on perfect safety is for people who don't have the balls to live in the real world.

—Mary Shafer, NASA Dryden Flight Research Center

Replies are listed 'Best First'.
Re^2: spliting text!!
by graff (Chancellor) on Mar 09, 2007 at 02:27 UTC
    Note: Code is not tested

    Here's a way to test it:

    $ perl -le '$s="1234567890"x20;@s=split(/(.){70}/,$s); print length($s);print scalar @s;print join("\n",@s)' 200 5 0 0 123456789012345678901234567890123456789012345678901234567890
    I'm not sure how to explain that output (5 array elements, first and third are empty strings, second and fourth are "0", fifth is 60 characters), but part of the reason might be evident from a slight change -- put the capturing close-paren after the "{70}":
    $ perl -le '$s="1234567890"x20;@s=split(/(.{70})/,$s); print length($s);print scalar @s;print join("\n",@s)' 200 5 1234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890 123456789012345678901234567890123456789012345678901234567890
    That's a little better, but not quite what the OP was looking for, I think. Hard to be sure exactly what the OP was looking for, but having the first and third array elements empty (again) is probably not part of the plan.

    The first element is empty because an empty string "occurs before" the first string that matches the split regex. The second array element is the part that matched the split regex (because of the parens). The third is empty, because that's what occurs before the split regex matches again. Fourth element is the second string to match the split regex. Last element is the remainder (split regex didn't match anymore).

    Maybe you meant something like:

    split /(?<=.{70})(.{70})/ # pos.look-behind for 70 chars, then captur +e 70
    (updated to fix incorrect comment)

    Let's test that:

    $ perl -le '$s="1234567890"x20;@s=split(/(?<=.{70})(.{70})/,$s); print length($s);print scalar @s;print join("\n",@s)' 200 3 1234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890 123456789012345678901234567890123456789012345678901234567890
    I'm not sure if that's a "better" solution than what other monks have proposed above, but it seems to work. ;)
Re^2: spliting text!!
by duff (Parson) on Mar 09, 2007 at 14:34 UTC

    Actually, IMHO, this is almost a perfect example of when not to use split. :-)