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

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

by BrowserUk (Patriarch)
on Mar 01, 2003 at 05:18 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-26 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found