From: Mathew Snyder <[EMAIL PROTECTED]>
> I'm trying to create an array of hashes with hash names that are simply
> "user1",
> "user2", "user3", etc. How do I use this method when adding one of the hashes
> to the array?
>
> my $user = "user" . $i;
>
> (build hash...)
>
> push @userData, hash
>
> Would it be %$user? %{$user}? Some other variation on that theme?
You do not need to do anything like that. The 'my' variables are
block-scoped, not procedure scoped in Perl (unlike AFAIK C or C#).
This means that you can do something like
while (<>) {
my %hash;
my @row = split ',', $_;
$hash{name} = $row[0];
$hash{other} = $row[3] / $row[4];
push @userData, \%hash;
}
since you get a brand new hash with a different address in every
iteration of the loop.
HTH, Jenda
P.S.: Well, you get a different address if you keep references to
those hashes. If you use the hash within the body of the loop, but no
reference to it remains after each iteration then most likely Perl
will reuse the memory. But that's irrelevant. As far as you are
concerned, you get a brand new empty unrelated hash each time.
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/