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

completely new to Perl. I have been struggling all day with the concept of references in hashes (ie when to use it?). my current problem is I made a hash that stores 3 elements: x, y, and an array associated with that coordinate.
$lengths{$line}= {x=> $site[0], y=>$site[1], z=> \@data};
I don't have an issue assigning the hash and using Data::Dumper I see that z is a reference. However, I cannot seem to retrieve the array to manipulate. Say I wanted to print each element in this array:
for my $coord (keys %lengths) { for (@{$lengths{$coord}{z}}) { print "$_\n"; } }
This gives me nothing. Is it because I am forcing scalar? Sample output from Data::Dumper
$VAR57 = '10:36759286'; $VAR58 = { 'reads' => $VAR2->{'reads'}, 'x' => '10', 'y' => '36759286'
edit: forgot the sigil thanks.

Replies are listed 'Best First'.
Re: Help with array ref in hashes
by kennethk (Abbot) on Jan 09, 2015 at 21:26 UTC
    Your posted code fails compile with the error
    Missing $ on loop variable
    Please make sure that when you post code, it literally replicates your issue -- see How do I post a question effectively?.

    If I run the code:

    my %lengths = ( 1 => {x => 1, y => 2, z => [1,2,3], }, 2 => {x => 2, y => 3, z => [2,3,4], }, 3 => {x => 4, y => 5, z => [5,6,7], }, ); for my $coord (keys %lengths) { for (@{$lengths{$coord}{z}}) { print "$_\n"; } }
    I get the output
    1 2 3 5 6 7 2 3 4
    which seems to be your spec. My best guess is that you've got a typo in the code you are actually running, so that you aren't accessing the array. Try checking Data::Dumper after your loop to see if you are getting bitten by autovivification.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Help with array ref in hashes
by toolic (Bishop) on Jan 09, 2015 at 21:23 UTC
    Your loop looks ok, but maybe we need to see your dumper output:
    use warnings; use strict; my %lengths = (c1 => {z => [1..5]}); for my $coord (keys %lengths) { for (@{ $lengths{$coord}{z} }) { print "$_\n"; } } __END__ 1 2 3 4 5

    A good reference:

Re: Help with array ref in hashes
by AnomalousMonk (Archbishop) on Jan 09, 2015 at 21:45 UTC

    I, too, seem to be getting the output I think you want to get from the OPed code (with missing  $ sigil restored):

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $line = 'foo'; my @data = qw(hi ha ho); my @site = (9, 8, 7); my %lengths; ;; $lengths{$line} = { x => $site[0], y => $site[1], z => \@data }; dd \%lengths; ;; for my $coord (keys %lengths) { for (@{$lengths{$coord}{z}}) { print $_; } } " { foo => { x => 9, y => 8, z => ["hi", "ha", "ho"] } } hi ha ho


    Give a man a fish:  <%-(-(-(-<

      ok thanks to everyone for your help. i just realized i made a dumb mistake that I was passing the reference \@data to z but since it is a temporary variable I was using to pass data I was erasing it everytime. I guess I need to for loop the storage of @data into my hash.