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

Hi thanks for that. I've tested it and I can't seem to get that to work. At the moment my code for it looks like this:

my @chunks = ($string =~ /(?|(?:((?:[^\n]*\n){19}[^\n]*)\n)|((?s:.*))) +/g); print Dumper @chunks[0];

That returns the message:

Sequence (?|...) not recognized in regex; marked by <-- HERE in m/(?| + <-- HERE (?:((?:[^\n]*\n){19}[^\n]*)\n)|((?s:.*)))/.

Wouldn't know where to start with fixing that I'm afraid.

Thanks.

Replies are listed 'Best First'.
Re^3: Split a string at a given line number
by JavaFan (Canon) on May 12, 2010 at 12:50 UTC
    It seems you're running an old Perl. (?|) was introduced in the previous major version of Perl, 5.10, which was released 2.5 years ago.

    You may consider upgrading to 5.10, or even 5.12.

      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!

        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 ' ];
        If you do want to keep the newlines, I would use (untested):
        my @chunks = $string =~ /(?:[^\n]*\n){1,20}/g;