CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
multidimensional hash problems

 



tg
Novice

May 11, 2009, 1:44 AM

Post #1 of 6 (473 views)
multidimensional hash problems Can't Post

Hello,

I am attempting to push data to a multidimensional hash but I seem to be
overwriting the key value pairs.

Here is a code snippet:


Code
        my %vals; 

$tmp = "@values";
@tmpval = split(/\*/, $tmp);
foreach my $tmpval (@tmpval) {
@tmpdata = split(/\s/, $tmpval);
@hname = @tmpdata;
# Extract the first value in each array entry to use as hash name
my $hname = shift(@hname);
foreach my $tmpdata (@tmpdata) {
$rec = {};
$vals{$hname} = $rec;
($key, $value) = split /=/, $tmpdata;
$rec->{$key} = $value;
}
}


Thanks


FishMonger
Veteran / Moderator

May 11, 2009, 8:02 AM

Post #2 of 6 (468 views)
Re: [tg] multidimensional hash problems [In reply to] Can't Post

perldoc -f push


FishMonger
Veteran / Moderator

May 11, 2009, 8:35 AM

Post #3 of 6 (461 views)
Re: [tg] multidimensional hash problems [In reply to] Can't Post

Why respond to my post in a PM?

Quote
I should rephrase. I am trying to load data into an empty hash so do I still need to use the push function?


Please post the contents of the @values array and how you want that data stored in the hash.


tg
Novice

May 11, 2009, 9:03 AM

Post #4 of 6 (458 views)
Re: [FishMonger] multidimensional hash problems [In reply to] Can't Post

Sorry, meant to reply to the list... not PM.

Here's a small portion of the @values array:

*studentid=4565745 class=junior grad=yes*studentid=7896546 class=senior grad=no*studentid=3456456 class=soph grad=yes

The HoH is loading up the way I want but only the very last values are present.

Here's the Data::Dumper output of the hash:


Code
$VAR1 = { 
'studentid=4565745' => {
'grad' => 'yes'
},
'studentid=7896546' => {
'grad' => 'no'
},
'studentid=3456456' => {
'grad' => 'yes'
}
};


Thanks


FishMonger
Veteran / Moderator

May 11, 2009, 10:02 AM

Post #5 of 6 (437 views)
Re: [tg] multidimensional hash problems [In reply to] Can't Post

This is clearly a homework assignment, so I can't show you the most efficient solution, but I can guide you a little.

These lines are not needed.

Code
$rec = {}; 
$vals{$hname} = $rec;


And this one needs to be altered.

Code
$rec->{$key} = $value;



tg
Novice

May 11, 2009, 10:33 AM

Post #6 of 6 (433 views)
Re: [FishMonger] multidimensional hash problems [In reply to] Can't Post

Thanks for the tip.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives