Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Hash of array.

by Nansh (Acolyte)
on Feb 09, 2017 at 09:08 UTC ( [id://1181497]=perlquestion: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re: Hash of array.
by Discipulus (Canon) on Feb 09, 2017 at 09:18 UTC
    Hello Nansh

    First of all please reformat your post seconding Writeup Formatting Tips

    Then go to read perldsc to see many recipes about complex datastructures

    The HashesOfArrays section has well written example about the generation and access of such datastructures.


    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Hash of array.
by kcott (Archbishop) on Feb 09, 2017 at 09:20 UTC

    G'day Nansh,

    "I need to get values from hash and need to push that in an array. ... $hash_one{$name}=@arr;"

    That's an assignment to a hash: you've got it the wrong way around:

    push @arr, $hash_one{$name};

    — Ken

Re: Hash of array.
by Laurent_R (Canon) on Feb 09, 2017 at 11:07 UTC

    a quick example under the Perl debugger:

    DB<1> %hash = ( jan => 1, feb => 2, mar => 3); DB<2> push @array, $hash{$_} for keys %hash; DB<3> x \@array; 0 ARRAY(0x20275150) 0 1 1 3 2 2
      push @array, $hash{$_} for keys %hash;

      That will work (as shown) but to me seems very unwieldy when we could use values instead:

      push @array, values %hash;

      Does using for and keys buy you anything here?

        You're right, hippo++. I used that construct for pedagogical purpose because I thought at the time that looping over the keys to pick up the values was showing in a better way what's going on, but, thinking again about it, it's true that using values or keys is just the the same idea after all, and that using values makes it possible to use a simpler syntax, be is in a for loop as my post above or, even better, in a direct array assignment.
Re: Hash of array.
by AnomalousMonk (Archbishop) on Feb 09, 2017 at 18:38 UTC

    Nansh: I find your OPed question a bit vague, but I think the answers I've seen so far are probably pretty close to the mark.

    However, it occurs to me that you might be thinking about a slice:

    c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; my @arr = qw(one two three); ;; my %hash; @hash{ qw(uno dos tres) } = @arr; dd \%hash; " { dos => "two", tres => "three", uno => "one" }

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

Re: Hash of array.
by madtoperl (Hermit) on Feb 09, 2017 at 09:45 UTC
    If you are trying to push the values from hash into an array and retrieve it back, you can try this

    #!/usr/bin/perl use strict; use warnings; my @array = (); my %hashcont = ( first => 0, second => 5, last => 8 ); push( @array, \%hashcont ); my $hash_ref = $array[0]; my %hash_data = %{$hash_ref}; print "$hash_data{first} $hash_data{second} $hash_data{last}\n";

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1181497]
Approved by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-05-26 18:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found