in reply to Help with Hash of hashes, is there a better way?

Anything that:
...perfectly describes the information i'm working with...
is not
Nasty
:-)

Also, I wouldn't call four while loops to traverse a complex data structure unweildy but ymmv.

You have chosen, imo, good meaningful field/var names and I think this produces readable code. It's very easy to see what you are doing.

If the amount of data is large and you need to build/load/store this structure frequently I might consider a relational db, say, MySQL. You would have a nice collection of left join tables.

Perl is often lauded for it's regexes. I would argue that the ability to easily handle these types of complex data structures comes in at a close second.

update:
Fixed some typos/grammer

  • Comment on Re: Help with Hash of hashes, is there a better way?

Replies are listed 'Best First'.
Re^2: Help with Hash of hashes, is there a better way?
by TeraMarv (Beadle) on Jun 01, 2006 at 08:11 UTC

    wfsp,

    I suppose i'm not happy with the nested loops as i have to traverse the structure several times in the code and that requires using the same bit of code again and again. Any bit of code that gets used more than once is crying out for a function to be written. Unfortunately either my skills,imagination or experience isn't quite up to it.

    Any ideas?

      Option 2)

      A callback would be simpler:

      { iterate { my ( $env_name, $platform_name, $host_name, $target_name, $total_capacity, $free_capacity, ) = @_; local $, = "\t"; local $\ = "\n"; print $env_name, $platform_name, $host_name, $target_name, $total_capacity, $free_capacity; } $stuff; }

      Tested.

        Thanks ikegami,

        I like it, very cool. Something I would never have come up with myself....well at least not this year ;-)

        That's what I love about perlmonks, you always learn something useful.

        Thanks again....to all Monks

      Option 1)

      You could build an iterator, seperating the traversal from the client as demonstrated by the following:

      { my $iter = get_target_iter($stuff); while (my ( $env_name, $platform_name, $host_name, $target_name, $total_capacity, $free_capacity, ) = $iter->()) { local $, = "\t"; local $\ = "\n"; print $env_name, $platform_name, $host_name, $target_name, $total_capacity, $free_capacity; } }

      Tested.

      Update: Below is an alternative iterator. It's a drop-in replacement for the above function. This version is much smaller thanks to Algorithm::Loops's NestedLoops.

      Tested.

      that requires using the same bit of code again and again.

      Whenever I see this phrase, deafening alarm bells and sirens go off in my head. In the vast majority of cases, any time you're using the same (or similar) chunk of code over and over again, you can probably do better with a subroutine. In the case of a sub for traversing a complex data structure, you may want to use a callback hook to provide the specific functionality.

      Some thoughts.

      • I could live with 'several' and not worry too much. :-)
      • You could do everything in one pass.
      • You could consider putting your traversing loops into a sub and pass a sub ref for the particular work that needs to be done.

      HTH