in reply to Re^3: Split a string at a given line number
in thread Split a string at a given line number

Would be great, but I'm working on a company system here. Nothing can be upgraded without permissions and even with them 'free' software has a different definition for industry! Am trying to work with the one below, which works fine, but it removes the newlines from the string (which I might need to keep). Will keep battling with this!
  • Comment on Re^4: Split a string at a given line number

Replies are listed 'Best First'.
Re^5: Split a string at a given line number
by AnomalousMonk (Archbishop) on May 12, 2010 at 16:11 UTC

    Tested with AS 5.8.9, should work (but untested) with pre-5.8:

    >perl -wMstrict -le "my $s = qq{line 1\nline 2\nline 3\nline 4\n}; my $l = 2; my @sra = $s =~ m{ (\A (?: [^\n]* \n){$l}) (.*) }xms; use Data::Dumper; print Dumper \@sra; " $VAR1 = [ 'line 1 line 2 ', 'line 3 line 4 ' ];
Re^5: Split a string at a given line number
by JavaFan (Canon) on May 12, 2010 at 13:11 UTC
    If you do want to keep the newlines, I would use (untested):
    my @chunks = $string =~ /(?:[^\n]*\n){1,20}/g;