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

I would definitely go with BrowserUK's idea. Since you are numbering anyway, an array is perfect for you. Basically, the "3.1" is only important because it comes after "2.*" and before "4.*", so there's really no reason to put it in a hash, but rather an array is good.

BrowserUK: Does your example have one to many levels of anonomous arrays? I would think it would more be something like

@structure = ( { ... }, [ { ... }, { ... }, ], [ { ... }, { ... }, ], [ { ... }, { ... }, ], );

That way you can do something like:

my @structure = ( $hashref_from_first ); foreach @query { ... push @structure, [] foreach @subquery { ... push @structure->[0], $hashref; } }

Or something like that (but probably a lot better)

    -Bryan

Replies are listed 'Best First'.
Re^3: Best complex structure?
by BrowserUk (Patriarch) on Jun 08, 2005 at 18:25 UTC

    As I mentioned in the post, it wasn;t clear to me if the OP needed to retain the 2.1/2.2/2.3 etc. levels arising from the multiple executions of the second and third statements with different bound parameters. Hence I added an extra level to retain them and noted that they could be removed if that information (count by parameter) was not required.


    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 these sharp comments! I am glad to see perl gurus on this question.

      I will try to implement this structure. Actually I don't need to retain the order between 2.1 and 2.2 for instance. Sorry if it was not clear enough!

      If I use the fetchall_arrayref, maybe it is not worth to change this structure?
      does perl spend a lot of resources to create a strucure?
      Can I come back on this page to add new comments if I need help to implement it or to get some value? There is so much topics posted each day that I assume it is difficult to check if there is a new comment for an old node (except if you look carefully the newest nodes section)

      thanks again.
        Thanks for these sharp comments!

        You're welcome.

        Can I come back on this page to add new comments if I need help to implement it or to get some value?

        You would be most welcome to.

        There is so much topics posted each day that I assume it is difficult to check if there is a new comment for an old node (except if you look carefully the newest nodes section)

        The simple answer is to become a member! It costs nothing, take maybe 1 minute to do, and then each time you log in, the server will show you a message telling you if you have any replies to any of your posts. It's so easy, there really is no reason not to.

        Alternatively, if you really can't or don't want to become a member, the when you post a node (especially a top-level (new) question) make a note it's number. In this case it's 456165. Then, the next time you check back, type that number into the search box at the top left corner of the screen and click the button, and you will be taken directly to the root node and you can scan down for any new replies.

        But really. Join! You are missing out on some of the nidest bits of PM by not doing so. For example, when you look at Newest Nodes as an Anonymous Monk, you get to see the last 24 hrs worth, but if you are a member, it only shows you those that you haven't already read (or decided don't interest you).


        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.
        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.