BioNrd has asked for the wisdom of the Perl Monks concerning the following question:
I apologize, sometimes i forget that monks are mere mortals, not programing gods that can read my mind.
The advice to slow down and think goes a long way. I appreciate that greatly. I solved the one issue relating to getting the hash reference, but still have the issue with the flexible naming. See comments inside the code. I may have a solution, but I am not confident in it.
use Data::Dumper; use warnings; my @array_1 = (1 .. 10); my @array_2 = (1 .. 10); my $internal_hash_number = 2; for (1 .. $internal_hash_number) { my $internal_name = $_; my @hold_a; my @hold_b; my $min = 10; my $max = 50; for (1 .. 10) { my $internal_value_a = int rand($max - $min + 1) + $min; push(@hold_a, $internal_value_a); my $internal_value_b = int rand($max - $min + 1) + $min; push(@hold_b, $internal_value_b); } $internal_hash{$internal_name} = { "$internal_name\_a" => [@hold_a], "$internal_name\_b" => [@hold_b], }; } $storage_hash{1} = { holder => [@array_1], individual => [ @array_2 ], marker => [ %internal_hash ], }; print Data::Dumper->Dumpxs([\%storage_hash], [q{*storage_hash}]); #now: #this accesses the array within the hash: my @external_array = @{$storage_hash{1}{'holder'}}; print "@external_array\n"; #this accesses the hash within the hash: my %internal_hash_ref = @{$storage_hash{1}{'marker'}}; my $internal_ref_name = '1_a'; my @internal_array_ref = @{$internal_hash_ref{1}{$internal_ref_name}}; print "@internal_array_ref\n"; @internal_array_ref[1] = 20; print "@internal_array_ref\n"; $internal_hash_ref{1}{$internal_ref_name} = [@internal_array_ref]; print Data::Dumper->Dumpxs([\%internal_hash_ref], [q{*internal_hash_re +f}]); # This gives me the array back out that I can now do things with. # BUT I want to be able to have a code that is flexable... # So that I can change the number of $internal_hash_number... # and the code will be... # smart enough to name the array based on... # what it is called in the hash. my $value = 0; for (1 .. $internal_hash_number) { $value++; my $safe_input = $value; print "$safe_input\n"; @{$safe_input}=(); push @{$safe_input}, @{$internal_hash_ref{1}{$internal_ref_name}}; print "@{$safe_input}\n"; } #do you monks think that this is a valid bit of code? #or should I not do it like this?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HwithinHoA? Complex Data Structure...I am so lost...
by wfsp (Abbot) on Dec 14, 2007 at 07:50 UTC | |
|
Re: HwithinHoA? Complex Data Structure...I am so lost...
by perlfan (Parson) on Dec 14, 2007 at 05:39 UTC | |
|
Re: HwithinHoA? Complex Data Structure...I am so lost...
by johngg (Canon) on Dec 14, 2007 at 11:09 UTC | |
|
Re: HwithinHoA? Complex Data Structure...I am so lost...
by wfsp (Abbot) on Dec 15, 2007 at 08:59 UTC |