Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Sort Algorithm (recursive?)

by Roy Johnson (Monsignor)
on Oct 14, 2005 at 15:23 UTC ( [id://500258]=note: print w/replies, xml ) Need Help??


in reply to Sort Algorithm (recursive?)

For the general case, you'd need a recursive solution to traverse the pred lists. For this particular case, you don't have to worry about something being a pred-of-a-pred. I believe I have fixed the recursive section, as well.
my %hash=( 'MEX1J' => { desc => 'Job 2' , pred => ['TEX1J'], }, 'MEX2J' => { desc => 'Job end', pred => ['TROUBLE'], }, 'TEX1J' => { desc => 'Job start', pred => [], }, 'TROUBLE' => { desc => 'who cares', pred => ['MEX1J'] } ); sub pred_cmp { my ($i1, $i2) = @_; # Anybody with no preds is first if (@{$hash{$i1}{pred}} == 0) { return -1; } elsif (@{$hash{$i2}{pred}} == 0) { return 1; } # If one is a pred of the other, it's first elsif (grep {$_ eq $i2} @{$hash{$i1}{pred}}) { return 1; } elsif (grep {$_ eq $i1} @{$hash{$i2}{pred}}) { return -1; } else { # Check recursively my ($rec) = grep $_, map {pred_cmp($_, $i2)} @{$hash{$i1}{pred +}}; if ($rec) { print "Returning $rec\n"; return $rec } ($rec) = grep $_, map {pred_cmp($i1, $_)} @{$hash{$i2}{pred}}; if ($rec) { print "Returning $rec\n"; return $rec } return 0; } } for (sort {pred_cmp($a,$b)} keys %hash) { print "$_\n"; }

Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://500258]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found