Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Ordering hash replacements to avoid clobbering things (update chaining)

by BrowserUk (Pope)
on Mar 01, 2003 at 05:18 UTC ( #239659=note: print w/replies, xml ) Need Help??

in reply to Ordering hash replacements to avoid clobbering things (update chaining)

Somewhat belated, but I had fun exploring different methods. Whether this is better in any way I doubt, but somebody might find it interesting.

#! perl -slw use strict; use Data::Dumper; sub unwindDependancies{ my ($hashref) = @_; my %copy = %{$hashref}; my @keys = keys %$hashref; my @deps; for (@keys) { next unless exists $copy{$_}; push @deps, [ $_ ]; while( exists $hashref->{$_}) { unshift( @{ $deps[-1] }, $_ = $hashref->{$_} ); @{$deps[-1]} > @keys and die('Circular reference found inv +olving:', "@{$deps[-1]}"); delete $copy{$_}; } } my %h; for (@deps) { my $r = \%h; $r = exists $r->{$_} ? $r->{$_} : ($r->{$_} = {}) for @$_; } #print Dumper \%h; my (@order, @stack); my $r=\%h; my $t=0; { while(my ($key, $val) = each %$r) { push @order, $key; push(@stack, $r), $r = $val, next if keys %$val; } $r = pop(@stack), redo if @stack; } # print "@order"; return @order; } my %test = ( qw[A B B C D E E F F G G C J H H F I H K H N M + O P P Q Q N S U T V]); print join' ', unwindDependancies \%test; my %replace = ( qw/ COM SEC MOB HIS MOC COM ICM1 INO ICM2 INO EU HIS CY GRE AE MOB IN ICC GR GRE MH MOC CO MOC MO HIS / ); print join' ', unwindDependancies \%replace; __DATA__ C:\test>238721 V T C G F H I J K E D B A U S M N Q P O ICC IN SEC COM MOC CO MH HIS EU MO MOB AE GRE CY GR INO ICM1 ICM2

Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.
  • Comment on Re: Ordering hash replacements to avoid clobbering things (update chaining)
  • Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://239659]
[Discipulus]: tunashish is a smoking test?
[oiskuu]: .oO( Lots of poorly tuned pianos await in thezip's hell. )
[thezip]: Apparently I have already reached my destination...
[erix]: pianola's :P
[oiskuu]: Prepare your piano with tuna and your cat will show you some love.
thezip has a sudden compusion to dance the tango...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2017-11-17 19:28 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (272 votes). Check out past polls.