Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Hash of Hashes

by BioNrd (Monk)
on Oct 08, 2007 at 12:57 UTC ( #643449=perlquestion: print w/replies, xml ) Need Help??

BioNrd has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am interested in creating a hash of hashes that will contain variables that are set by the user.

I have put this together to create a hash of a variable (population size, @total) that is sent into this subroutine. $tester is also sent into the array and is the number of populations that the user creates.

sub splitter { my %hasher = (); my $increase = 0; my $input = ""; while ($tester--) { $increase ++; push @key, $increase; } @hasher{@key} = @total; foreach $key (sort { $a <=> $b } keys %hasher) { print "Population #$key = $hasher{$key}\n"; } while () { print "Enter the population you will act on: "; chomp ($input = <STDIN>); $output = $hasher{$input}; #references a specific value in the + hash. print "$output BEFORE \n"; $output = $output + 8; print "$output AFTER\n"; $hasher{$input} = $output; #returns the modified value to the +hash foreach $key (sort { $a <=> $b } keys %hasher) { print "Population #$key = $hasher{$key}\n"; } return $output; } }

Later in the code, I take the $output value and turn it into an array that is modified, and i want to then return it to the hash in the following format:
A: Population number
1. Listing of individuals from 1n (user defined) ie. @array = (1 $population);
2. Listing of genetic marker associated with each individual (not yet coded)

The other thing I wanted to do, is be able to modify the arrays at the lower level of the hash, as I have done for my simple hash above. Something I have no idea how to do.

I have only been at this two weeks, and this is my first attempt at programming. Any help on any aspect of this post would be greatly appreciated. Thanks monks!

Replies are listed 'Best First'.
Re: Hash of Hashes
by snopal (Pilgrim) on Oct 08, 2007 at 13:13 UTC

    A Hash of Hashes will be in this form:

    $hasher{$key}{'population'} = $population; $hasher{$key}{'g_marker'} = $genetic_marker;

    You won't be able to use that fast hash assignment as you have written to populate it, but it will be much more flexible in the range of data it can contain. You can still do everything else.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://643449]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2020-11-29 10:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?