Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Take $1-$100 and put it into a hash

Status
Not open for further replies.

Kirsle

Programmer
Jan 21, 2006
1,179
US
This is a small little question:

How do you take the variables $1 to $100 (as matched through regexp) and put them into a hash, where the keys of the hash would be 1 to 100?

I've tried a few things, such as:
Code:
for (my $i = 1; $i <= 100; $i++) {
   # Tried this
   $stars{$i} = \${$i};

   # And this
   $stars{$i} = ${$i};

   # And this
   $stars{$i} = \$i

   # And none of them worked.

   # This was the only solution I could find:
   $stars{$i} = eval ('$' . $i);
}

It seems like there should be an easy solution without having to resort to eval.
 
Wow - that's some regexp!

You can do it when you run the regexp itself:
Code:
my %stars;
@stars{ 1 .. 100 } = $some_variable =~ /some_regexp/;
 
Thanks! I knew there had to be a simple one-liner explanation to this one. ;)
 
all you had to do was:

Code:
for (my $i = 1; $i <= 100; $i++) {
   $stars{$i} = "\$$i";
}

but do your really want to add the dollar sign like that?
 
I think you've missed the point Kev. That gives the literal string "$1", "$2", etc. The OP is looking for the values of the first 100 capturing groups in a regexp.
 
Amsterdam, should be ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top