Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How to get out an array of a hash?

by buchi2 (Acolyte)
on Aug 03, 2017 at 08:09 UTC ( [id://1196614]=perlquestion: print w/replies, xml ) Need Help??

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

Hello!

In article 1195995 I got it work with your help to build an array of hashes.
Now I want to get out a whole array of it, but it is always empty.

#!/usr/bin/perl -w use strict; use warnings; my %hierhash; my @feld; my @hashfeld; my $wert; my @array; @hashfeld = (); %hierhash = &dofn(0,5,1); print "1 $hierhash{'var2'}[3] \n"; push @hashfeld, {%hierhash}; %hierhash = &dofn(0,5,2); print "2 $hierhash{'var2'}[3] \n"; push @hashfeld, {%hierhash}; $wert = $hashfeld[0]->{'var2'}[3]; print "$wert\n"; $wert = $hashfeld[1]->{'var2'}[3]; print "$wert\n"; @array = (); @array = $hashfeld[1]->{'var2'}; print "array $array[3]\n"; # ################################################ # holt die fn Werte # ################################################ sub dofn { my $i; my ($ivon, $ibis, $imal) = @_; my %hashfn; for ($i = $ivon; $i < $ibis; $i++) { push @{$hashfn{'var1'}}, $i*$imal; push @{$hashfn{'var2'}}, $i*$imal*10.; push @{$hashfn{'var3'}}, $i*$imal*20.; } return %hashfn; }
What is going wrong with it?

Regards, Buchi

Replies are listed 'Best First'.
Re: How to get out an array of a hash?
by shmem (Chancellor) on Aug 03, 2017 at 09:02 UTC
Re: How to get out an array of a hash? (Updated)
by thanos1983 (Parson) on Aug 03, 2017 at 08:21 UTC

    Hello buchi2,

    You are getting an array of arrays, I have altered the output so you can see it easier.

    From there it is easy (I guess) on how to get the array or at least you can search online on how to extract an array from an array of arrays ;)

    A few minor recommendations regarding your code (very briefly). You do not need to add the -w on the shebang, -w it is an alternative of using use warnings;. Also you do not need to use the & on your subroutines see here why What is the point of the & / ampersand sigil for function refs?.

    Update: I used Data::Dumper to debug the output, I think you will find it extremely useful, at least I do. :)

    Update2: Fellow monk hexcoder provided you already an answer to your question so I thought it would be nice also to add some minor notes on your code. In Perl you do not need to predefine your variables you can define them the moment you assign them. Also I found very useful the feature say. It is like print but it add an new line character \n at the end your string or what ever attribute you are calling.

    Update3: One more reference to read from a similar question Dereference array of arrays.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: How to get out an array of a hash?
by hexcoder (Curate) on Aug 03, 2017 at 08:25 UTC
    Hi, you need to change the access to an array reference $hashfeld[1]->{'var2'}. Dereference it in order to get the referenced values .
    @array = $hashfeld[1]->{'var2'};
    =>
    @array = @{$hashfeld[1]->{'var2'}};
Re: How to get out an array of a hash?
by Anonymous Monk on Aug 03, 2017 at 09:20 UTC

    In  sub dofn  { you write 'var1', 'var2', 'var3'

    'var1', 'var2', 'var3' is spelled @array

    my @fn; push @{ $fn[1] }, 1.11; push @{ $fn[2] }, 2.1; push @{ $fn[3] }, 3.1; Data::Dump::dd(\@f);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found