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

Hello monks, Spent sometime in searching how to do, but unable to. So kindly forgive if the question is too silly. How could i split out the following string into arrays based up on the specified string... String to be splitted,
some junk DELIMITING_TEXT test1 test2 test3 test4 DELIMITING_TEXT test1 test2 test1 test2 test1 test2 DELIMITING_TEXT test1 test2 DELIMITING_TEXT some junk
each set of text between DELIMITING_TEXT into an array element, how to do that ?

Replies are listed 'Best First'.
Re: split based upon string
by biohisham (Priest) on Feb 04, 2010 at 10:02 UTC
    Changing the $/ variable to the value of "DELIMITING_TEXT" would make the "DELIMITING_TEXT" be removed by chomp when reading in, however, read perlvar to see caveats involving "$/"... when printing this array, each element of the array can be spread on some new lines because "\n" wasn't used as the line separator while reading the data in...
    #!/usr/local/bin/perl use strict; use warnings; local $/ = "DELIMITING_TEXT"; my @array; while(<DATA>){ chomp; push @array, $_; } foreach my $element(@array){ print "$element"; # no "\n" is typed } __DATA__ some junk DELIMITING_TEXT test1 test2 test3 test4 DELIMITING_TEXT test1 test2 test1 test2 test1 test2 DELIMITING_TEXT test1 test2 DELIMITING_TEXT some junk


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: split based upon string
by cdarke (Prior) on Feb 04, 2010 at 10:01 UTC
    use warnings; use strict; local $/ = 'DELIMITING_TEXT'; my @array = <DATA>; chomp @array; local $" = '|'; print "@array\n"; __DATA__ some junk DELIMITING_TEXT test1 test2 test3 test4 DELIMITING_TEXT test1 test2 test1 test2 test1 test2 DELIMITING_TEXT test1 test2 DELIMITING_TEXT some junk
    What you do not say is what you want to do with the new-lines at the end of each data item. A for loop is probably called for to remove them, but replace them with what?
      This looks alot like a MIME mail message format. Have you searched CPAN for a MIME parser. Like "MIME::Parser".
Re: split based upon string
by arc_of_descent (Hermit) on Feb 04, 2010 at 10:06 UTC
    Also read the documentation for the split function. You can pass a regex or a string as the delimiter.
    my @parts = split 'DELIMITING_TEXT', $entire_string;

    --
    Rohan
      You can pass a regex or a string

      Just keep in mind that using single quotes around the pattern (instead of /.../) does not make it a plain string — it's still a regex.  Although this is not relevant here, it might make a difference in other cases:

      #!/usr/bin/perl -l my $s = 'xABCxACxABBCxAB*Cx'; print join(' - ', split('AB*C', $s)); # x - x - x - xAB*Cx
Re: split based upon string
by ambrus (Abbot) on Feb 04, 2010 at 10:49 UTC

    You could try something like

    my(@all, $cur); while (<DATA>) { if ($_ =~ /\ADELIMITING_TEXT\Z/) { push @all, $cur; $cur = ""; } else { $cur .= $_; } } push @all, $cur;

    The output is in @all then.