in reply to Re: Returning hash reference from a sub
in thread Returning hash reference from a sub

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.
  • Comment on Re^2: Returning hash reference from a sub

Replies are listed 'Best First'.
Re^3: Returning hash reference from a sub
by Corion (Patriarch) on Mar 18, 2013 at 13:59 UTC
    ... %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 => '' };

        That's not how hashes work. A key can only appear once in a hash.

        Hi, this won't work as you use the same keys.

        $VAR1 = { 'names' => 'mary', 'names' => 'john', 'names' => 'Doe', 'names => '' };
        Better try to make an array of hashes which would look like this:
        $VAR1 = [ { 'names' => 'mary'}, { 'names' => 'john'}, { 'names' => 'Doe'}, { 'names => ''} ];
        or
        $VAR1 = { 'names' => ['mary','john','Doe','']}; ];