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

Two part question:

PartA:

@value = {$hoh{$newcount}{'individual'}}; print "@value \n";
This should reference values generated in the HoH below.
... { $HoH{$counter} = { individual => [ @newind ], chromasome1 => [ @newchroma ], chromasome2 => [ @newchromb ], }; } ...
However, it gives me HASH(0x180bef4) rather then the list I want, and throws this warning: Odd number of elements in anonymous hash at (I can't follow previous discussion they are greek to me)...I figure the answer is something simple, I just can't figure out what the simple thing is.

PartB: Once this works can I then modify the @value, and replace @newchroma with @value? Sounds simple to say...but I am not seeing how this is easy to do. Thanks monks...

Replies are listed 'Best First'.
Re: Why is this not what I think it is...
by GrandFather (Saint) on Nov 06, 2007 at 01:32 UTC

    use strict; use warnings;. You use %hoh in the first code sample and %HoH in the second.

    {$HoH{1}{'individual'}} tries to generate an anonymous hash but only provides one element - a hash requires pairs of elements. Very likely you intended my @value = @{$HoH{$counter}{'individual'}}; to cast the array reference to an array.

    Possibly the overall code you are looking for is something like:

    use strict; use warnings; my %HoH; my @newind = (1 .. 3); my @newchroma = (4 .. 6); my @newchromb = (7 .. 9); $HoH{1} = { individual => [@newind], chromasome1 => [@newchroma], chromasome2 => [@newchromb], }; my @value = @{$HoH{1}{'individual'}}; print "@value \n"; $value[0] = 42; $HoH{1}{'chromasome1'} = [@value]; print "@{$HoH{1}{'chromasome1'}}\n";

    Prints:

    1 2 3 42 2 3

    Perl is environmentally friendly - it saves trees
Re: Why is this not what I think it is...
by graff (Chancellor) on Nov 06, 2007 at 03:49 UTC
    You're pretty close. I think you just forgot a necessary "@" sigil in the assignment to @value. Try it like this:
    @value = @{$hoh{$newcount}{'individual'}};
    Here's a little one-liner cook-book example to demonstrate the concept:
    perl -MData::Dumper -e '%h=(one=>{a=>[1,2,3],b=>[4,5,6]},two=>{b=>[5,6 +,7],c=>[7,8,9]}); print Dumper(\%h); @a=@{$h{one}{a}}; print Dumper(\@a)'
    When I run that, I get:
    $VAR1 = { 'one' => { 'a' => [ 1, 2, 3 ], 'b' => [ 4, 5, 6 ] }, 'two' => { 'c' => [ 7, 8, 9 ], 'b' => [ 5, 6, 7 ] } }; $VAR1 = [ 1, 2, 3 ];
Re: Why is this not what I think it is...
by BioNrd (Monk) on Nov 06, 2007 at 14:24 UTC
    Thanks for the help. It is a learning process. I am surprised I have gotten this far (a lot farther with the help). Speaks wonders for the ease of perl as a language that someone with no experience can pick it up in a months time.