Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Referencing/returning array of hashes from sub

by toolic (Bishop)
on Jun 08, 2011 at 18:06 UTC ( #908756=note: print w/replies, xml ) Need Help??

in reply to Referencing/returning array of hashes from sub

my (@date, @F_SCORE, @F_SCORE_BOX,
This assignment makes @date gobble up all the values on the RHS, leaving the other arrays empty. A smaller example will illustrate this:
use warnings; use strict; use Data::Dumper; my ($date_ref) = [0..3]; my ($F_SCORE_ref) = [5..7]; my (@date, @F_SCORE) = (@$date_ref, @$F_SCORE_ref); print Dumper(\@date); print Dumper(\@F_SCORE); __END__ $VAR1 = [ 0, 1, 2, 3, 5, 6, 7 ]; $VAR1 = [];

From perldata:

You can actually put an array or hash anywhere in the list, but the first one in the list will soak up all the values, and anything after it will become undefined.
You should consider a different data structure: perldsc

Replies are listed 'Best First'.
Re^2: Referencing/returning array of hashes from sub
by PoGGiE (Initiate) on Jun 08, 2011 at 18:23 UTC
    Thanks. I think I understand; makes sense if all the returns are sequential in memory and @date has no idea how big it 'should' be so gobbles everything up. What data structure would you recommend?



      Dereference your references as you need to work with them. There's no syntactic need to expand them into non-references. Instead of:

      my @expanded = @$reference; for my $element (@expanded) { ... }

      ... you can just as well write:

      for my $element (@$reference) { ... }
        Thanks, I've managed to fix my problem now (at last!). Thanks for the help.

        Scrapping the big
        my (@date, ...) = (@$date_ref)

        and replacing with
        my @date = @$date_ref; my @F_SCORE = @$F_SCORE_ref; my @F_SCORE_BOX = @$F_SCORE_BOX_ref; my @F_ROA = @$F_ROA_ref; my @F_CFO= @$F_CFO_ref; my @F_dROA = @$F_dROA; my @F_ACCRUAL = @$F_ACCRUAL_ref; my @F_dLEVER = @$F_dLEVER_ref; my @F_dLIQUID = @$F_dLIQUID_ref; my @F_EQOFFER = @$F_EQOFFER_ref; my @F_dTURN = @$F_dTURN_ref; my @F_dMARGIN = @$F_dMARGIN_ref;

        did the trick (although I'm not sure why).

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2022-08-12 17:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found