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

Hi, I have create a hoh which I am passing to a subroutine no problems. eg
$clocks{$pin_name}{'period'} = $Worksheet->{Cells}[$row][$clock_col]-> +Value; #snip foreach my $key (sort keys %clocks){ print_clock_assertions($clocks{$key},$key); } sub print_clock_assertions { my($hashref)=$_[0]; my($clock_name)=$_[1]; if($hashref->{'virtual'} eq "VIR") { etc...
however I've tried to put and array into the hash and pass that to the array but I am having no luck. Any indicators as to what I'm doing wrong?
if ($Worksheet->{Cells}[$row][$jit_ear_rise_col]){ my @jit_values; foreach my $col ($jit_ear_rise_col .. $jit_late_fall_col){ push @jit_values,$Worksheet->{Cells}[$row][$col]->Value; } $clocks{$pin_name}{'jit'} = @jit_values; } my @jitter_values = $hashref->{'jit'} ; foreach (@jitter_values){print "$_\n";}

Replies are listed 'Best First'.
(ar0n) Re: hashes of hashes of arrays in subroutine
by ar0n (Priest) on May 11, 2001 at 18:54 UTC
    Almost:
    $clocks{$pin_name}{'jit'} = \@jit_values;
    You needed to assign an arrayref as value...

    Update upon further inspection...
    my @jitter_values = $hashref->{'jit'};
    should be
    my @jitter_values = @{ $hashref->{'jit'} };
    What @{ ... } does is dereference the arrayref into an ordinary list.

    ar0n ]

      Thanks ++:-)

      I guess I still dont fully understand the perl references cause I keep making mistakes like this :-)

      Diarmuid

      Just be aware that taking a reference to the array means that it will always be *THAT* array. If you change the array, you change the thing that's pointing to it. Brackets make a new copy. (see above =o)
Re: hashes of hashes of arrays in subroutine
by rchiav (Deacon) on May 11, 2001 at 19:23 UTC
    FWIW - I had the same delima when I started working with complex data structures. Not that merlyn needs any more compliments :), but I found this article of his to be very helpful in decyphering refrences.

    The basic jist is that by using @{$foo->{'bar'}}, you're derefrencing the value of 'bar' as a list.. and you can work with it as a list because the corresponding value of 'bar' is a refrence to a list; not a list in and of itself.

    Hope this helps..
    Rich

Re: hashes of hashes of arrays in subroutine
by Anonymous Monk on May 11, 2001 at 21:45 UTC
    Try installing it with brackets (the anonymous array reference constructor), like so:
    $clocks{$pin_name}{'jit'} = [ @jit_values ] ;
    Good luck. =o)