[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-beginners
Subject:    Re: Iterated Hash Names
From:       "Jeff Pang" <rwwebs () gmail ! com>
Date:       2007-09-30 15:42:27
Message-ID: ddb1c4550709300842q5275c50dna02a4dcaca995684 () mail ! gmail ! com
[Download RAW message or body]

2007/9/30, Mathew Snyder <theillien@yahoo.com>:
> 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
>

Why not create the anonymous hashs?like,

for my $i (1 .. 3) {
    push @userData {'user' .$i => 'value1', 'key2' => 'value2' ...};
}

or maybe a HoH is better,as,

my %HoH = ( 'user1' => { key1 => value1,key2 => value2...},
                     'user2' => { key1 => value1,key2 => value2...}
                    ...
                   );

If you really want to use a variable as hash's names,you may need to
eval a string,which is not good so.

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic