in reply to How to remove the $1 hard coding

You could do
{ no strict 'refs'; $somestring =~ m/\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*/; $result = ${$matchnum}; }
but it is strongly discouraged (forget about it immediately), and won't even work under strict (that's the reason for the no strict 'refs';). Fletch's answer provides a much better solution, to which I could just add that you can avoid copying the matches to an array, if you treat the return values of the match as a list:
push @sps, (m/\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*/)[$columnNumber];
should work fine.
Cheers, CombatSquirrel.

Replies are listed 'Best First'.
Re: Re: How to remove the $1 hard coding
by hardburn (Abbot) on Aug 22, 2003 at 16:44 UTC

    Actually, after thinking about it, I don't think a symbolic ref solution would be so bad in this case, provided you take a few percautions. At the top of your subroutine, make sure $matchnum matches against /\A\d+\z/ and that $$matchnum is a defined value after the match (change your last line to $result = $$matchnum if $$matchnum;).

    Though overall, you're still probably better off with the array solution.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      You meant, of course,   $result = $$matchnum if defined $$matchnum;.   And while I agree the array approach is better, your mention of $$matchnum "made me look".   I hadn't thought the $1 variables were _that_ global.