Date: Fri, 29 Jun 2001 06:13:24 -0400 From: Benjamin Goldberg Subject: Re: Naming a hash Message-Id: <3B3C54C4.BD290F98@earthlink.net> Just in wrote: > > Dear group, > > Suppose I was in a loop, and I wanted to push some values into a hash, > but I wanted the hash to have a different name for each iteration of > the loop. i.e how can I do this:- > > for ($a = 0; $a <= $#SomeArray; $a++) > { > # do something to get data here > > $NewHash$a{$Var} = $Val; > } > > Obviously this wont work, and variations of the theme (that I have > attempted), dont work either. > > I would really appreciate if someone were to enlighten me. What you're asing for is how to do symrefs. As mentioned, symrefs are evil, and another method should be used [eg, a hash of hashes, or an array of hashes]. However, if you *really* want to know, here's how to do it: for my $a ( 0 .. $#SomeArray ) { ${"NewHash$a"}{$Var} = $Val; } Note that the above will not work if "use strict" is in your program. Personally, I would [probably] use an array of hashes instead, though. for my $a ( 0 .. $#SomeArray ) { $NewHash[$a]{$Var} = $Val; } -- The longer a man is wrong, the surer he is that he's right.