Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
After an hour an a half, I convinced my co-worker he was full of contradictory statements and what he said he needed wasn't at all the correct definition of the problem. After figuring out what he really wanted to do, it turned out to be a simple matter of constructing a "depends on me" tree and then using the topological sort.
#!/usr/bin/perl use strict; use warnings; my %all_pkg; my %want_rm; my %must_keep = map {$_ => 1} grep {! $want_rm{$_}} keys %all_pkg; my %tree; for my $pkg (keys %all_pkg) { push @{$tree{$_}}, $pkg for dependencies($pkg); } PACKAGE: for my $pkg (keys %want_rm) { my @rm_order = how_to_uninstall($pkg); for (@rm_order) { if ($must_keep{$_}) { print "Can't remove $pkg - depends on $_ which is a must k +eep\n"; next PACKAGE; } } print "Safe to remove $pkg\n"; for (@rm_order) { print "\tRemoving $_\n"; remove_pkg($_); } } sub dependencies { my ($pkg) = @_; # Functionality to return list of dependencies } sub remove_pkg { my ($pkg) = @_; # Functionality to remove a package } { my %seen; my %onstack; my @list; sub how_to_uninstall { my $target = shift; (@list, %seen, %onstack) = (); _traverse($target); return @list; } sub _traverse { my $x = shift; $seen{$x} = $onstack{$x} = 1; for my $y (@{$tree{$x}}) { die "cyclic!" if $onstack{$y}; _traverse($y) unless $seen{$y}; } push @list, $x; $onstack{$x} = 0; } }

Cheers - L~R

In reply to Re^4: Algorithm For Uninstalling Dependencies by Limbic~Region
in thread Algorithm For Uninstalling Dependencies by Limbic~Region

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (6)
    As of 2018-07-18 20:47 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (395 votes). Check out past polls.