Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Sort Algorithm (recursive?)

by BrowserUk (Patriarch)
on Oct 14, 2005 at 15:22 UTC ( [id://500257]=note: print w/replies, xml ) Need Help??


in reply to Sort Algorithm (recursive?)

This does the job for your simple case with the only recursion necessary is that embedded inside Perl's builtin sort.

UpdateHowever, if your structure could have circular dependancies that are indirect--ie. A depends on B depends on C depends on A.--then this will not detect that.

Making it more efficient is the next challenge, but maybe that doesn't matter if your hash is smallish.

Updated to use @{} instead of $#{}. With thanks to Roy Johnson

#! perl -slw use strict; my %hash = ( MEX1J => { desc => 'Job 2', pred => [ 'TEX1J' ], }, MEX2J => { desc => 'Job end', pred => [ qw[TEX1J MEX1J] ], }, TEX1J => { desc => 'Job start', pred => [], } ); my @orderedKeys = sort{ return 0 unless @{ $hash{ $a }{ pred } } or @{ $hash{ $b }{ pred } }; my $AinB = grep /$a/, @{ $hash{ $b }{ pred } }; my $BinA = grep /$b/, @{ $hash{ $a }{ pred } }; die "Circular dependacy $a:$b" if $AinB and $BinA; return -1 if $AinB; return 1 if $BinA; return 0 unless $AinB or $BinA; } keys %hash; print "$_ $hash{ $_ }{ desc } @{ $hash{ $_ }{ pred } }" for @orderedKeys; __END__ P:\test>500240 TEX1J Job start MEX1J Job 2 TEX1J MEX2J Job end TEX1J MEX1J

A somewhat more efficient version curtesy of the Orcish Manouver(?) and List::Util::first that avoids modifying your datastructure.

#! perl -slw use strict; use List::Util qw[first]; my %hash = ( MEX1J => { desc => 'Job 2', pred => [ 'TEX1J' ], }, MEX2J => { desc => 'Job end', pred => [ qw[TEX1J MEX1J] ], }, TEX1J => { desc => 'Job start', pred => [], } ); my %cache; my @orderedKeys = sort{ return 0 unless @{ $hash{ $a }{ pred } } or @{ $hash{ $b }{ pred } }; my $AinB = $cache{ "$a|$b" } ||= first{ /$a/ } @{ $hash{ $b }{ pre +d } }; my $BinA = $cache{ "$b|$a" } ||= first{ /$b/ } @{ $hash{ $a }{ pre +d } }; die "Circular dependacy $a:$b" if $AinB and $BinA; return -1 if $AinB; return 1 if $BinA; return 0 unless $AinB or $BinA; } keys %hash; print "$_ $hash{ $_ }{ desc } @{ $hash{ $_ }{ pred } }" for @orderedKeys; __END__ P:\test>500240 TEX1J Job start MEX1J Job 2 TEX1J MEX2J Job end TEX1J MEX1J
</reduce>

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found