Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Returning hash reference from a sub

by tobias_hofer (Friar)
on Mar 18, 2013 at 13:47 UTC ( #1024041=note: print w/ replies, xml ) Need Help??


in reply to Returning hash reference from a sub

Hi,

I am usually directly using references to anonymous hashes, like this:

while ( my $row = $data_handle->fetchrow_hashref ){ my $row_data = {}; # get a fresh hash for the row data # fill in this row $row_data->{names} = $row->{'names'} || '';
And at the end I simply do return $row_data this is working fine for me.
Cheers
Tobias


Comment on Re: Returning hash reference from a sub
Select or Download Code
Re^2: Returning hash reference from a sub
by Anonymous Monk on Mar 18, 2013 at 13:57 UTC
    Yes, but I am still stuck with only one row of data, I need to get all the data from the while loop and return it from the sub.
      ... %final_data = @loop_data; ...

      What is this line supposed to do?

      Please also explain how you intend to use the data stored in %final_data.

      Maybe you wanted to use the following construct instead?

      return { names => \@loop_data };
        Need to store the data in a hash to return from the sub, if Dumper:
        $VAR1 = { 'names' => 'mary', 'names' => 'john', 'names' => 'Doe', 'names => '' };

        The line %final_data = @loop_data will take the array and map the elements:

        my @a = ('H','e','l','o'); my %fus = @a;
        so you will get
        %fus: H -> e, l -> o
        In case you assign an odd list to %final_data the code will break!
        What about crating and array of hashes? and return the reference to the array?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024041]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-09-02 22:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (31 votes), past polls