Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have an issue in spliting a string. The problem is I have to split only by every 6 time the delimiting factor occur. Is that possible.?

 $data = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";

Here I need to give coma as the delimiting factor, but I need to split only after 5 comas.

means the output should be split like the below,

"A,B,C,D,E"

"F,G,H,I,J" & so on

Replies are listed 'Best First'.
Re: Seeking help with split!
by AnomalousMonk (Archbishop) on Jun 30, 2013 at 19:43 UTC
    ... split only by every 6 time the delimiting factor occur. [emphasis added]
    >perl -wMstrict -le "my $data = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'; ;; my @ra = split m{ (?: , [^,]+){5} \K , }xms, $data; ;; printf qq{'$_' } for @ra; " 'A,B,C,D,E,F' 'G,H,I,J,K,L' 'M,N,O,P,Q,R' 'S,T,U,V,W,X' 'Y,Z'

    Or, alternatively (didn't read this far before posting the first solution), ...

    ... split only after 5 comas. ... output should be ... like ... "A,B,C,D,E" "F,G,H,I,J" & so on [emphasis added]
    >perl -wMstrict -le "my $data = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'; ;; my @ra = split m{ (?: , [^,]+){4} \K , }xms, $data; ;; printf qq{'$_' } for @ra; " 'A,B,C,D,E' 'F,G,H,I,J' 'K,L,M,N,O' 'P,Q,R,S,T' 'U,V,W,X,Y' 'Z'

    \K is available with Perl version 5.10+. See discussion of  \K in Look-Around Assertions (sub-section on "(?<=pattern)" "\K") in perlre.

      Thanks AnomalousMonk.

      I have another doubt now. How can we split the below with the delimiter "','" with same 4 times?

      my $data = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z';

      So that output will be like,

      'A','B','C','D','E'     'F','G','H','I','J'       'K','L','M','N','O'       'P','Q','R','S','T'  'U','V','W','X','Y'      'Z'

      I tried your code, but not working with "','"

        nah, it is your code that's not working. You assign a list to a scalar (not meant to store a list, see perlintro) and perl, trying what's possible, takes the first scalar value off that list and stores it in $data. Check that variable:

        my $data = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'

        print $data; A

        See?
        So what was your intention? Use an array? Put quotation marks into a string? I cannot tell from your question.

        Cheers, Sören

        (hooked on the Perl Programming language)

Re: Seeking help with split!
by AnomalousMonk (Archbishop) on Jul 01, 2013 at 18:48 UTC

    As Happy-the-monk said, your requirements are unclear. If you have a string with a bunch of single-quotes in it, try this (using a different delimiter for the string):

    >perl -wMstrict -le "my $data = q{'A','B','C','D','E','F','G','H','I','J','K','L','M'}; ;; my @ra = split m{ (?: , [^,]+){3} \K , }xms, $data; ;; printf qq{:$_: } for @ra; " :'A','B','C','D': :'E','F','G','H': :'I','J','K','L': :'M':

    If you actually have an array to begin with, try this, which gives you an array-of-arrays (see  natatime() in List::MoreUtils; another approach might be to use splice):

    >perl -wMstrict -le "use List::MoreUtils qw(natatime); use Data::Dumper; ;; my @data = ('A','B','C','D','E','F','G','H','I','J','K','L','M'); ;; my $it = natatime 4, @data; my @groups; while (my @group = $it->()) { push @groups, \@group; } print Dumper \@groups; " $VAR1 = [ [ 'A', 'B', 'C', 'D' ], [ 'E', 'F', 'G', 'H' ], [ 'I', 'J', 'K', 'L' ], [ 'M' ] ];