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

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.

Replies are listed 'Best First'.
Re^8: Best complex structure?
by mat21 (Beadle) on Jun 10, 2005 at 09:21 UTC
    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
      In my case, I found this solution, because $structure is not declared.

      Ah! That means you do not have strict and warnings enabled in your code?

      Whilst it is fine to ask naive questions (at least once and maybe even twice :), not using strict and warnings is unacceptable when you are asking for help.

      Why? Because each time you find a problem, and post code asking for help, the first thing I (and most others) are going to do is add strict and warnings and run perl -c yourscript in order to let the compiler tell us what errors you are making. Then we would have to post a reply pointing these problem out to you.

      Why should we do this, when the compiler can tell you about your mistakes directly?

      The compiler may seem pedantic and complain about a lot of things that you seem to be able to get away with by ignoring them, but each of those warnings is there for a very good reason: That of helping you write better code.

      If you choose to not use strict and warnings, you are effectively saying that you know better than the compiler. In which case, you don't need my help, because I always let the compiler point out my errors.

      This code

      push @structure , []; for (my $i=0;$i<= $#ex; $i++) { push @{ @structure->[$j]}, $ex[$i]; }

      Produces a warning. But more importantly, demonstrates that you are misunderstanding something quite important. Enabling warnings and working out how to stop that warning being produced will force you to become aware of that misunderstanding. It is much better that you do this now, before you misunderstanding becomes a long term habit that will continue to bite you each time you make it.


      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.
        My explanation was maybe not accurate. (english is not my mother tongue sorry)
        Of course, I always use the pragma strict and warnings
        I must admit that I did not know -c but fortunatly my syntax is correct :)

        However your are right, I misunderstood something. let me just explain why, because I would not like that you keep in mind a wrong opinion.
        With $structure->[$j] I got an error message Global symbol "$struture" requires explicit package name
        I know what I mean. I have to declare a variable with "my". I did not understand why because I thougt that $structure->[$j] was declared with my @structure
        exactly like when you declare @array, you don't need to declare $arrray for using $arrray[$i].
        With @structure->[$j], I got a message explaning that it is deprecated but at the ends it was working.

        ok so my code is
        my @statement; ... my @structure; foreach (@statement) { ... push @structure , []; for (my $i=0;$i<= $#ex; $i++) { push @{ $structure->[$j]}, $ex[$i]; } }
        but for the moment I have still this error. I will be more rigourous before asking another question
        thanks
        edit: it works with $structure[$j]. There is sometimes some things which look like strange but I know that strange behaviour of perl come from the misunderstanding of the programmer :)
Re^8: Best complex structure?
by planetscape (Chancellor) on Jul 03, 2005 at 11:22 UTC