Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to create an array of hashes return form a subroutine

by gepebril69 (Beadle)
on Mar 26, 2012 at 15:00 UTC ( #961681=perlquestion: print w/ replies, xml ) Need Help??
gepebril69 has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I like to create an array of hashes from returned hashes from a subroutine. I know how to create an array of hashes by e.g.:
my @NameArray; $NameArray[0]{firstname} = "Jan"; $NameArray[0]{famname} = "Jansen"; $NameArray[0]{street} = "ParkAvenue";
But what is I get an hash returned from a subroutines with new firstname, famname, street results, like
sub addnaw { # In real a subroutine which collects a specific line # from a file my %value $value{firstname} = "John"; $value{famname} = "Johnson"; $value{street} = "Bellpark"; return %values; }
And I would like to add it directly to that array like
my @NameArray; for ($i; $i < $endvalue; $i++) { NameArray[$i] = addnaw(); # Fault code }
Which does not work, is it possible, or do I have to use more code.

Comment on How to create an array of hashes return form a subroutine
Select or Download Code
Re: How to create an array of hashes return form a subroutine
by roboticus (Canon) on Mar 26, 2012 at 15:15 UTC

    gepebril69:

    Return a hash reference, which you can put in your array. See perldsc

    sub addnaw { return { # anonymous hash reference a=>'foo', b=>'bar' } }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: How to create an array of hashes return form a subroutine
by tangent (Deacon) on Mar 26, 2012 at 15:16 UTC
    You need to store a reference to each hash in the array, try this:
    my @NameArray; my $endvalue = 5; for (my $i = 0; $i < $endvalue; $i++) { $NameArray[$i] = addnew($i); } for my $hash_ref (@NameArray) { print "$hash_ref->{firstname} $hash_ref->{famname}\n"; } sub addnew { my $index = shift; my %value; $value{firstname} = "John $index"; $value{famname} = "Johnson $index"; $value{street} = "Bellpark $index"; return \%value; # return a reference to the hash }
      @Tangent, Thanks very much, so I was close, just missed an '\' @All others thanks for the help as well.
Re: How to create an array of hashes return form a subroutine
by stevieb (Hermit) on Mar 26, 2012 at 15:31 UTC
    in haste, I mis-read the problem.
Re: How to create an array of hashes return form a subroutine
by JavaFan (Canon) on Mar 26, 2012 at 15:36 UTC
    for (my $i = 0; $i < $endvalue; $i++) { $NameArray[$i] = {addnaw()}; }
    Although it may be easier to have addnaw() return a hashref.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://961681]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-07-13 17:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (251 votes), past polls