Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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]
[hippo]: I had a low-paid job about 20 years ago and seriously considered going down to a 3-day week. Would have worked 60% of the time for about 80% of the cash.
[Corion]: ambrus: Well, I pay rent, but don't own a house with variable/ unforeseeable costs
[hippo]: The thesholds/benefits balance at the time was nuts.
[hippo]: But that doesn't last because, you know ... politicians. :(
[Corion]: hippo: Yeah, if you still get enough money to get by and don't have expensive hobbies or other fixed costs that can work out well
[ambrus]: ah good. I pay only rent too. but that still costs significant money.
[Corion]: ambrus: Yes, but that is already budgeted for

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2017-09-21 15:19 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (249 votes). Check out past polls.