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

Hi, I just got a crush curse in PERL, since I am working on small project. I am not familiar with PERL, but PHP.

I need to push data into the following PERL structure of hash-array combination. Basically I need to read from a spreadsheet values containing 'owner' information and link it to a specific directory. It can be done manually like this:

$directory{"owner1"}{0} = ["DIR1", "DIR2", "DIR3"]; $directory{"owner2"}{0} = ["DIR4", "DIR5", "DIR6"];

This works, but now I need to read it from the spreadsheet as the values come, and assign it to the specific 'owner' key Is there a way to push into a specific key? i.e:

push @directory{"owner1"} $value
This way it gets pushed into the specific array. Any help will be greatly appreciated. Thanks in advance.

Replies are listed 'Best First'.
Re: Pushing into a hash of arrays
by Arunbear (Prior) on Mar 16, 2015 at 17:25 UTC
    There is a way i.e.
    push @{ $directory{owner1} }, $value;
    or
    push @{ $directory{owner1}{0} }, $value;
    if there actually is an intermediate hash with key "0" that holds the array (I'm not sure why you need the intermediate hash).

    Updated: D'oh! Added missing commas in code samples (thanks hdb / choroba / davido !)

      Thanks to all for you answers! ;)
Re: Pushing into a hash of arrays
by sapphirecat (Acolyte) on Mar 16, 2015 at 19:45 UTC

    Hashes and arrays are different things in Perl. When you write $directory{"owner1"}{0} = ["DIR1", "DIR2", "DIR3"]; you are assigning a new arrayref to a hashref inside a hash. That is, %directory = ("owner1" => { "0" => [...] }); and $directory{"owner1"}{0}[0] eq "DIR1". (Note the curly-braces vs. square-brackets for hash/array access.) If you iterate over keys %{$directory{"owner1"}} then your keys will show up in an undefined order, not necessarily counting from 0 on up.

    I think you may need a comma in the push syntax as well as telling Perl you want to access the key as an array. Also assuming that you don't want the numerically-indexed hash in the middle layer, that would be:

    push @{ $directory{"owner1"} }, $value;

    That's how I always write it, anyway.

    "Basically, displaying invisible data is not maintainable."
Re: Pushing into a hash of arrays
by crusty_collins (Friar) on Mar 16, 2015 at 18:50 UTC
    if you already have the dirs out can do it like this
    use strict; use warnings; use Data::Dumper; my $directory ={}; @{$directory->{owner1} } = qw( dir1 dir2 dir3 ); @{$directory->{owner2} } = qw( dir4 dir5 dir6 ); print Dumper $directory;
    ouput
    $VAR1 = { 'owner2' => [ 'dir4', 'dir5', 'dir6' ], 'owner1' => [ 'dir1', 'dir2', 'dir3' ] };
Re: Pushing into a hash of arrays
by Mr. Muskrat (Canon) on Mar 17, 2015 at 18:47 UTC

    Hi, I just got a crush curse in PERL

    “Curse us and crush us, my precious is lost!” ― J.R.R. Tolkien

    The name of the language is "Perl" with only the first letter capitalized.

    A reply falls below the community's threshold of quality. You may see it by logging in.