Is that sample data you real data? How do you plan to solve the dependency loops (Task1 requires Task3, but Task3 requires Task1)? Also, Task2 requires Task2?
Anyways, my first thought is build a hash structure of the form
$deps{$task}{$requiredTask} .. so with the above you would have something like:
%deps = (
1 => { 2 => undef, 3 => undef },
2 => { 2 => undef, 4 => undef },
3 => { 1 => undef, 4 => undef },
5 => { 2 => undef, 3 => undef },
);
So now, to see if X requires Y, you can just do
exists $deps{$X}->{$Y}, or to list the deps (1 level) do
keys %{$deps{$X}} . From there you can loop through listing dependencies or make a recursive rountine to find all levels (but be very careful of the cyclic deps).
To build the above data structure, it could just be something like:
my %deps;
while(<STDIN>){
my @cols = split;
my $task = shift @cols;
$deps{$task}->{$_} = undef for @cols;
# or another way:
%{$deps{$task}} = map { $_ => undef } @cols;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.