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

I want to save the field I'm splitting on - in this case the pattern is ewh12345 (initials followed by numeric). Hanging off of this pattern is other data - the records are variable length. I want to split on this pattern (i.e., A-ZA-Z\d\d\d\d\d) but also want to be able to include this pattern along with the data associated with it. Any suggestions?

Replies are listed 'Best First'.
Re: saving a split pattern
by moritz (Cardinal) on Nov 03, 2008 at 17:07 UTC
    If you use parenthesis around your pattern, it will appear in the returned list:
    use strict; use warnings; my $s = 'PRELUDExz3456MIDDLEzy1234POST'; my @list = split m/([a-z]{2}\d{4})/, $s; print "@list\n"; __END__ PRELUDE xz3456 MIDDLE zy1234 POST
      just wanted to note the alternative, if the pattern should not be in an extra element:
      my @list = split m/(?=[a-z]{2}\d{4})/, $s;
      output:
      PRELUDE xz3456MIDDLE zy1234POST