in reply to Using split() to divide a string by length

Another trick that works is to capture the split characters, which places them also in @fields and makes pos advance beyond them. Since all but probably the last group match, the normal split results mostly don't contain anything, so we need to filter out false elements with grep:

my $string = join '', a..z; my @fields = grep {$_} split /(.{3})/, $string; print "@fields\n"; __END__ abc def ghi jkl mno pqr stu vwx yz

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Using split() to divide a string by length
by japhy (Canon) on Apr 13, 2006 at 19:51 UTC

      Yep, I tried with defined first because that's the way I thought it worked, too. With that, the result of mine is,

      abc def ghi jkl mno pqr stu vwx yz
      Note the extra spaces, indicating that there are defined empty strings instead of undefs in those positions.

      Update: Good idea, ikegami++.

      After Compline,
      Zaxo

        Then grep length, ... is needed.