in reply to how to dynamically declare a varialble?

You want to use a hash for this.

Update: Jenda pointed me to the actual question.

Sampath - you want to assign the matches to an array. Something along the lines of:

my $string = "aabbccdd"; if (my @matches = $string =~ /(aa)..(cc)../) { print "@matches\n"; print "$matches[0] ... $matches[1]\n"; }

The difference is that $1 will be in $matches[0], and so forth.

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested

Replies are listed 'Best First'.
Re: Re: how to dynamically declare a varialble?
by Jenda (Abbot) on May 22, 2004 at 19:16 UTC

    No he does not. Looks like someone read just the node subject and not the text :-)

    If he was trying to declare (well actually USE) variables with ad hoc names, you'd be right, but he just needs to access the regexp matches. I think in this case the easiest solution is to turn the strict refs off in the block. Or rewrite the code so that he gets the matches as a list. Which is not always possible without huge changes to the code.

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      use strict; my %hash = ( 1 => \$1, 2 => \$2 ); my $hash = 1; "aa bb cc" =~ /(aa)/ and print ${ $hash{$hash} },$/; __END__ aa
      Ahhh. I missed the phrase "matched memory variable". I will reply again.

      Your solution is poor - much better is to assign the matches to an array.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested

        Well it depends on what do you do. If you have code like this:

        if (/regexp/) { no strict 'refs'; my $i = 1; while (defined ${$i}) { ... } }
        then it's definitely better (and fairly easy) to change the code and assign the matches into an array. If you have something like:
        $text =~ s{regexp}{ no strict 'refs'; my $i = 1; while (defined ${$i}) { ... } ... }ge;
        it ain't that trivial.

        Jenda
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
           -- Rick Osborne

        Edit by castaway: Closed small tag in signature