Your question is a little vague. In the subject you ask about array slices, and in the example code and text of the question there is no mention of array slices. But I think I understand where you're going. Here are some illustrations. Forgive me for having taken liberties with your code. I did so with the objective of isolating the issues at hand.

First, interpolating an array into a character class in a regular expression.... You might think it to be as simple as /[@array]{n}/. But you would be wrong. Consider the following snippet:

use strict; use warnings; my @group1 = ('A','B','C','D'); my @group2 = ('E','F','G','H'); my $string="DEAB CDGHEF"; print "Match: $1\n" while $string =~ /([@group1]{4})/g;
Why on earth are you getting output of "AB C"? Well, remember that arrays interpolated into strings get 'list separation'. So you've interpolated the values of @group1 AND the space character (a few times) into the character class. The space gets in there as the record separator. That's easy to fix:
local $" = '';
placed before the regexp match will do it, but don't forget that the list separator has now been set to an empty string, and will remain so until "local" falls out of scope or you change it back. I used local in this case so that at very worst $" would revert back to its old self when its changed definition falls out of scope.

Since it's kinda ugly goofing around with $" just so that your regexp will work the way you want it to (will you really remember 6 months down the road that $" makes it so that your character class won't match space characters?) you should probably instead pre-concatenate the list contained in @group1 like this:

my @group1 = ('A','B','C','D'); my $char_class1 = join "", @group1; my $string="DEABCDGHEF"; print "Match: $1\n" while $string =~ /([$char_class]{4})/g;
Ok, that's taken care of. But what if you are dead set on interpolating an array into a regexp character class, and more specifically, you really want to interpolate an array slice (as the question asked)? Brace yourself, you need a really ugly construct known as ...... er... I can't remember what it's called, which is all for the best because you should forget you saw it. Here's how to use it inside of a character class. You're not going to like it:
use strict; use warnings; my @group1 = ('A','B','C','D'); my @group2 = ('E','F','G','H'); my $string="DEABCDGHEF"; local $" = ''; print "Match: $1\n" while $string =~ /([@{[@group1[0..2],@group2[2,3]]}]{5})/g;
If the sheer ugliness and unmaintainability of that construct doesn't scare you away from it you should probably keep your day job.

Here's how to do what you're trying to do without jumping through voodoo hoops:

use strict; use warnings; my @group1 = ('A','B','C','D'); my @group2 = ('E','F','G','H'); my $string="DEABCDGHEF"; my $group = join '', @group1[0..2], @group2[2,3]; print "Match: $1\n" while $string =~ /([$group]{5})/g;

Hope this helps! I'll leave it to you to adapt that final solution to your needs. It doesn't match the attempted functionality of your original code snippet, but learn from the technique; it should be the answer to your question, and can easily be applied to the snippet you used to illustrate your need.


Dave


"If I had my life to do over again, I'd be a plumber." -- Albert Einstein

In reply to Re: Using array slices during string matching by davido
in thread Using array slices during string matching by seaver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.