The approach of using list context for the match and the array slice:
my @fields = $data =~ m/^(\S+)\s+((\S+\s+)?(\S+))$/;
@values = @[EMAIL PROTECTED];

is exactly what I needed.

I still can't find this referenced in the docs though.

Thanks,
Don


On Tuesday 20 December 2005 09:30, Bob Showalter wrote:
> [EMAIL PROTECTED] wrote:
> > The issue:
> >
> > I have a routine which builds a regex and 'knows' which paren matches to
> > use afterwords.  For instance after a line such as:
> >
> > use strict;
> > my $data =~ m/^(\S+)\s+((\S+\s+)?(\S+))$/;
> >
> > the routine 'knows' it wants $1 and $4.  Another pass through the regex
> > will be different and the variables might be $2 and $5 for that regex.
> >
> > The needed variable numbers are in an array:
> > my @indexes;
> > @indexes = (1, 4); for the first example above and:
> > @indexes = (2, 5); for the second example above.
> >
> > The question:
> > Is there a way to reference the built-in variables: $1, $2, ... in a
> > programmatic manner?
> >
> > Something like:
> > @values;
> > push @values,${$_} foreach(@indexes);
> > which didn't work for me, but you get the idea.
>
> That should work if @indexes contains numbers like
>
>     @indexes = (1, 3);
>
> You'll also need to disable strict refs:
>
>     no strict 'refs';
>
> However, another approach would be to do your regex match in list
> context, and place the captured substrings into an array that you can
> index:
>
>    my @fields = $data =~ m/^(\S+)\s+((\S+\s+)?(\S+))$/;
>    @values = @[EMAIL PROTECTED];
>
> Here the first match is at index 0 (i.e. $1 is @values[0])
>
> > Yes, I already thought of a subroutine with a big ifels ladder that hard
> > codes a test for $1, $2, ... but there has to be a more eloquent solution
> > than this.
> >
> > Thanks,
> > Don


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to