Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Help with Hash of hashes, is there a better way?

by TeraMarv (Beadle)
on Jun 01, 2006 at 08:11 UTC ( [id://552994]=note: print w/replies, xml ) Need Help??


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

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?

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

Replies are listed 'Best First'.
Re^3: Help with Hash of hashes, is there a better way?
by ikegami (Patriarch) on Jun 01, 2006 at 09:24 UTC

    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

Re^3: Help with Hash of hashes, is there a better way?
by ikegami (Patriarch) on Jun 01, 2006 at 09:22 UTC

    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.

Re^3: Help with Hash of hashes, is there a better way?
by friedo (Prior) on Jun 01, 2006 at 09:03 UTC
    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.

Re^3: Help with Hash of hashes, is there a better way?
by wfsp (Abbot) on Jun 01, 2006 at 08:29 UTC
    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-23 20:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found