Recursion and a "been there" flag is a clean way to handle the problem:

use strict; use warnings; use Data::Dump; my %depends; while (<DATA>) { chomp; my ($job, @deps) = split ','; $depends{$job}{$_} = undef for @deps; } addDepends (\%depends, $_, keys %{$depends{$_}}) for keys %depends; print Data::Dump::dump(\%depends); sub addDepends { my ($depends, $target, @deps) = @_; for my $dep (@deps) { next if $depends->{$target}{$dep}++; addDepends($depends, $target, keys %{$depends{$dep}}); } } __DATA__ job1, job2,job1, job3,job2 job4,job2 job5,job2,job4 job6,jobz joba,job4,jobb jobz,jobbc,job2

Prints:

{ job1 => {}, job2 => { job1 => 1 }, job3 => { job1 => 1, job2 => 1 }, job4 => { job1 => 1, job2 => 1 }, job5 => { job1 => 2, job2 => 2, job4 => 1 }, job6 => { job1 => 2, job2 => 1, jobbc => 1, jobz => 1 }, joba => { job1 => 1, job2 => 1, job4 => 1, jobb => 1 }, jobb => {}, jobbc => {}, jobz => { job1 => 1, job2 => 1, jobbc => 1 }, }
Premature optimization is the root of all job security

In reply to Re: Multi level dependency structure by GrandFather
in thread Multi level dependency structure by MH1

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.