A better title for the OP's question would be "Finding all connected nodes in a directed graph". From the "members and partners" language, I'd guess that this is for something like a friend-of-a-friend networking tool

I'm not entirely sure what "extend the hash" means, but I think you might be well served by looking at the Graph::Base distribution.

Building the all-pairs shortest-path network might seem like overkill, but if you can find a way to cache that and only rebuild it when the network changes, you should then be able to repeatedly pull out whichever subset you're interested in.

use Graph::Directed; my $network = Graph::Directed->new(); # Build a directed network of partners and members foreach my $partner ( keys %partners ) { foreach my $item ( @{ $partners{$partner} ) { # Add each pairing as a network edge with weight 1 $network->add_edge( $partner, $item ); $network->set_attribute('weight', $partner, $item, 1); } } # Now build a shortest-path network my $friends = $network->APSP_Floyd_Warshall(); # Now review all of the links in the network my $required_level = 2; my ( @all_levels ) = $friends->edges(); while ( scalar @all_levels ) { my ( $from, $to ) = splice(@all_levels, 0, 2); my $degrees = $friends->get_attribute('weight', $from, $to); next if ( $degrees > $required_level ); push @{ $allmembers{$from} }, $to; }

At a minimum, take a look at the implementation of APSP_Floyd_Warshall, which seems to be related to what you're looking for.

I'm hardly a graph-theory expert, so the above might not really be what the OP was asking for... corrections would be welcome. I just knew that there must be an existing implementation for this and searched CPAN for Graph.


In reply to Re: creating a hash of arrays/hashes by simonm
in thread creating a hash of arrays/hashes by jonnyfolk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.