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

Re: Returning hash reference from a sub

by tobias_hofer (Friar)
on Mar 18, 2013 at 13:47 UTC ( [id://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

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-19 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found