Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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


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.

Replies are listed 'Best First'.
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 };

        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?

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024041]
[Corion]: Hi choroba, LanX!
[Corion]: LanX: Turn that frown upside down! ;)
[Eily]: hello people!
[choroba]: a bit early for harvest mite, isn't it?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2018-05-23 07:41 GMT
Find Nodes?
    Voting Booth?