Leser: 28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
use strict; use warnings; my %disk_space = ( host_A => { '/' => 0.22, '/var' => 0.51, '/opt' => 0.79, '/home' => 0.17, }, host_B => { '/' => 0.31, '/var' => 0.82, '/opt' => 0.99, '/home' => 0.22, }, ); printf( "%-10s:%-10s -> %.2f\n", @{$_} ) for sort { $a->[2] <=> $b->[2] } map{ my $host = $_; map{ [ $host, $_, $disk_space{$host}{$_} ] } keys %{ $disk_space{$host} }; } keys %disk_space;
2009-09-09T11:38:12 TaulmarillNach zwei Stunden Telefonkonferenz ist so eine Denksportaufgabe genau das richtige, daher hier mal ausnahmsweise eine fertige Lösung.
sort { $a->[-1] <=> $b->[-1] } flatten_hash( %disk_space )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %tree=( a=>[ {e=>1,f=>2}, {g=>3,h=>4,i=>5}, {j=>6}, ], b=>[ {k=>1,l=>2,m=>3,n=>4}, {o=>5,p=>6}, {q=>7,r=>8,s=>9}, ], c=>[ {t=>1,u=>2}, ], d=>[ {v=>1,w=>2,x=>3}, {y=>4,z=>5}, ], ); print Dumper(\%tree); print '-'x80,"\n"; my @l=flatten_tree(\%tree,'-'); print "$_->{key} = $_->{value}\n" for(@l); print '-'x80,"\n"; @l=sort{$b->{value} <=> $a->{value}}@l; print "$_->{key} = $_->{value}\n" for(@l); print '-'x80,"\n"; @l=sort{$a->{key} cmp $b->{key}}splice(@l,0,10); print "$_->{key} = $_->{value}\n" for(@l); print '-'x80,"\n"; my %restore=restore_tree(@l); print Dumper(\%restore); ######################################################################## ######################################################################## sub flatten_tree { my @l; my $ref=shift; my $sep=shift || ''; if($ref) { return () unless(_flatten_recursive($sep,\@l,$ref,[],[])); } return @l; } sub _flatten_recursive { my $sep=shift; my $base=shift; my $ref=shift; my @types=@{shift()}; my @path=@{shift()}; if(defined($ref) && ref($ref) eq 'ARRAY') { for my $num (0..$#$ref) { return 0 unless(_flatten_recursive($sep,$base,$ref->[$num],[@types,'ARRAY'],[@path,$num])); } } elsif(defined($ref) && ref($ref) eq 'HASH') { while(my ($key,$val)=each(%$ref)) { return 0 unless(_flatten_recursive($sep,$base,$val,[@types,'HASH'],[@path,$key])); } } else { push(@{$base},{ value=>$ref, key=>join($sep,@path), path=>\@path, types=>\@types, }); } return 1; } sub restore_tree { my @list=@_; if(@list) { my $ret; for my $elm (@list) { my @path=@{$elm->{path}}; my @types=@{$elm->{types}}; my $now; while( @path && @types ) { my $key = shift(@path); my $type = shift(@types); if($type eq 'ARRAY') { return () if($key!~/^\d+$/); $ret=[] unless($ret); $now=\$ret unless($now); $now=\$$now->[$key]; } elsif($type eq 'HASH') { return () unless($key); $ret={} unless($ret); $now=\$ret unless($now); $now=\$$now->{$key}; } else { return (); } } if($now) { $$now=$elm->{value}; } else { return (); } } return () unless($ret); return ref($ret) eq 'ARRAY'?@$ret:%$ret; } }
LanXich bin grad zu faul es zu coden