Hi,

I *think* this quick program does what you need:

use strict; use warnings; use Data::Dumper; my %dependencies; while (<DATA>) { chomp; my ($key, @items) = split /,/; $dependencies{$key} = [@items]; } # print Dumper \%dependencies; my $continue = 1; while ($continue) { $continue = 0; for my $key (keys %dependencies) { my @jobs = @{$dependencies{$key}}; my %seen = map { $_ => 1 } @jobs; my @add; for my $job (@jobs) { next unless exists $dependencies{$job}; my @new_jobs = @{$dependencies{$job}}; for my $new_job (@new_jobs) { next if exists $seen{$new_job}; $continue = 1; push @add, $new_job; $seen{$new_job} = 1; } } push @{$dependencies{$key}}, @add; } } print Dumper \%dependencies; __DATA__ job1, job2,job1, job3,job2 job4,job2 job5,job2,job4 job6,jobz joba,job4,jobb jobz,jobbc,job2
I have tested only with the simple example you provided and it seems to do what you need, but much more thorough testing would be needed. Although I have tried to take the case into account and to guard against it, you would need to make really sure that the program does not go into an infinite loop in the event of circular dependencies.

Below is the resulting data structure.

$VAR1 = { 'job3' => [ 'job2', 'job1' ], 'jobz' => [ 'jobbc', 'job2', 'job1' ], 'job6' => [ 'jobz', 'jobbc', 'job2', 'job1' ], 'job4' => [ 'job2', 'job1' ], 'joba' => [ 'job4', 'jobb', 'job2', 'job1' ], 'job5' => [ 'job2', 'job4', 'job1' ], 'job1' => [], 'job2' => [ 'job1' ] };

In reply to Re: Multi level dependency structure by Laurent_R
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.