Lamont85 has asked for the wisdom of the Perl Monks concerning the following question:
I have a database made up of .dat files that are formatted as follows:
UserID001|username1|password1|displayname1| UserID002|username2|password2|displayname2| UserID003|username3|password3|displayname3| ...
This is only one record of the database, and many other records will be formatted in this same fashion but with varying numbers of columns. I am trying to write a subroutine to accept each record as an array, then return a hash for each column using the UserIDs as the hash keys.
I'm assuming it will be best to return references to these hashes, but I'm not sure how to dynamicly create a hash for each column. Here's what I have so far:
# Calling the subroutine ($usernamesRef,$passwordsRef,$displaynamesRef) = arrayToMultiHash (\@u +sers); %usernames = %{$usernamesRef}; %passwords = %{$passwordsRef}; %displaynames = %{$displaynamesRef}; # The subroutine sub arrayToMultiHash { my @array = @{$_[0]}; my @hashRefs; foreach $line (@array) { my @lineInfo = split (/\|/,$line); my $key = shift (@lineInfo); my $i = 0; foreach $value (@lineInfo) { my $hashName = "hash$i"; $hashName{$key} = $value; $i++; } } foreach (0..$i) { my $hashName = "hash$i"; push (@hashRefs,\%{$hashName}); } return (@hashRefs); }
Any help will be greatly appreciated. Thank you in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating dynamic hash names
by Gangabass (Vicar) on Sep 24, 2009 at 11:57 UTC | |
|
Re: Creating dynamic hash names
by Bloodnok (Vicar) on Sep 24, 2009 at 11:50 UTC | |
|
Re: Creating dynamic hash names
by biohisham (Priest) on Sep 24, 2009 at 11:52 UTC | |
|
Re: Creating dynamic hash names
by leocharre (Priest) on Sep 24, 2009 at 14:39 UTC | |
|
Re: Creating dynamic hash names
by Anonymous Monk on Sep 24, 2009 at 14:41 UTC | |
|
Re: Creating dynamic hash names
by Lamont85 (Novice) on Sep 24, 2009 at 12:49 UTC |