in reply to Re^4: Best complex structure?
in thread Best complex structure?

If I use the fetchall_arrayref, maybe it is not worth to change this structure?

It's still much easier and cheaper to traverse an array in order than a hash. So, if your hash keys are numeric, you almost certainly should be using an array instead.

does perl spend a lot of resources to create a strucure?

Different structures have different costs. Hashes are "more expensive" than arrays.

If your dealing with a few hundred or a few thousand items, the differences aren't worth worying about.

It's only when you start dealing with very large datasets, several 100 thousands or more, that the difference can becomes important.

But, re-sorting a hash everytime you traverse it, even with a few hundred elements is sufficiently costly that if you can avoid doing so by using an array, it doesn't make sense to not use the array!


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Replies are listed 'Best First'.
Re^6: Best complex structure?
by mat21 (Beadle) on Jun 09, 2005 at 18:19 UTC
    For the moment I don't use fetchall_arrayref (even it is a better solution) because some strange errors concerning the validity of my SQL occurs. anyway I am trying to build my structure.
    @structure = ( [{ ... }, ], [ { ... }, { ... }, ], [ { ... }, { ... }, ], [ { ... }, { ... }, ], ); ## foreach @query { my @ex = executeRequest ($_); push @structure, []; for (my $i=0;$i<=$#ex;$i++) { push @structure->[$i], $ex[$i]; }
    and I got this message:
    Type of arg 1 to push must be array (not array element) at service.pm line 152, near "];"
    I am a bit confusing with some notation of references and I think I don't interpret properly @results->[$i]. is there another one (except @results[$i]) ?
    by the way, the sum function seems very interesting and I tested it.
    my @structure = ( [ # Result from first query. { 'field1_statement1' => 'value', 'field2_statement1' => 'value' }, ], [ #Results from second query { 'field1_statement2' => 'value', 'field2_statement2' => 'value', 'field3_statement2' => 'value' }, { 'field1_statement2' => 'value', 'field2_statement2' => 'value', 'field3_statement2' => 'value' } ], [ # results from third query { 'field1_statement2' => 'value' } ] ); my $q2Total = sum @{ $structure[ 1 ] }; print $q2Total."\n";
    and the results is ... 277446212

    I do think I missed something :)
    Sorry for these naive questions.

      Instead of

      push @structure->[$i], $ex[$i];

      You need

      push @{ $structure->[$i] }, $ex[$i];

      Which reads as; Use the $ith element of the array pointed at by the reference in the variable $structure as an array reference and then push the value of $ex[$i] onto that array.

      The basic form is that in ?{ $ref }, that if $ref contains a reference to an aggregate datastructure (array or hash) then ?{ $ref }, where ? is either @ or %, allows you to use that data structure. (Hmm. That probably doesn't clarify anything!)

      If $ref is an array ref, then @{ $ref } is the array it's referencing. Like wise for hashrefs and %{ $ref }.

      There are shortcut versions of this syntax. If the reference is a simple scalar then you can omit the curlies: @$ref, but when the reference is itself a member of an aggregate, that doesn't work. I tend to use the full @{ $ref } syntax always. For both consistancy and because I don't have to remember when the shortcur does and doesn't work.

      To get a better understanding of using references see perlreftut, perdsc and/or do a Super Search for "Tutorials references" here at PM.

      Sorry for these naive questions.

      Don't be. Leastwise, not for my sake. The only people who get upset about naive questions are those that either a) received their knowledge as a result of genetic imprinting; b) have forgotten that they once didn't know what they now know.

      In either case, it's just intellectual snobbery--the very worst kind.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
        Thanks for that!
        Your explanation is clear and  push @{ $structure->[$i] }, $ex[$i]; In my case, I found this solution, because $structure is not declared.
        push @structure , []; for (my $i=0;$i<= $#ex; $i++) { push @{ @structure->[$j]}, $ex[$i]; }
        now I am looping the structure like that
        foreach my $lev1 (@structure) { my $cnt = scalar(@{$lev1}; foreach my $lev2 (@{$lev1}) { my %hash = %{$lev2}; foreach my $key ( keys %hash ) { # if ($key eq $xx) etc. } } }
        Is there a way more straigthforward to fetch the key and the value? maybe wih the map function