Thread Array of hashs
(20 answers)
Opened by
roli
at 2005-03-02 23:06
User since 2003-08-04
5873
Artikel
ModeratorIn
Hier das zweite Beispiel:
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
#!/usr/bin/perl use strict; use warnings; use Storable qw//;
use Data::Dumper; $Data::Dumper::Sortkeys = 1;
my %h1 = ( a => { inhalt => 1 }, b => { inhalt => 2 }, c => { inhalt => 3 }, d => { inhalt => 4 }, ); print "Adresse h1 -> ", \%h1, "\n"; print "Adresse h1_a -> ", $h1{a}, "\n"; print 'h1 : ', Dumper \%h1;
my $h2 = \%h1; print "Adresse h2 -> ", $h2, "\n"; print "Adresse h2_a -> ", $h2->{a}, "\n"; $h2->{c}->{inhalt} = 33; print 'h1 : ', Dumper \%h1; print 'h2 : ', Dumper $h2;
my $h3 = { %h1 }; print "Adresse h3 -> ", $h3, "\n"; print "Adresse h3_a -> ", $h3->{a}, "\n"; $h3->{d}->{inhalt} = 44; print 'h1 : ', Dumper \%h1; print 'h3 : ', Dumper $h3;
my $h4 = Storable::dclone \%h1; print "Adresse h4 -> ", $h4, "\n"; print "Adresse h4_a -> ", $h4->{a}, "\n"; $h4->{a}->{inhalt} = 11; print 'h1 : ', Dumper \%h1; print 'h4 : ', Dumper $h4;
Ausgabe:
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
Adresse h1 -> HASH(0x1a45948) Adresse h1_a -> HASH(0x15d5230) h1 : $VAR1 = { 'a' => { 'inhalt' => 1 }, 'b' => { 'inhalt' => 2 }, 'c' => { 'inhalt' => 3 }, 'd' => { 'inhalt' => 4 } }; Adresse h2 -> HASH(0x1a45948) Adresse h2_a -> HASH(0x15d5230) h1 : $VAR1 = { 'a' => { 'inhalt' => 1 }, 'b' => { 'inhalt' => 2 }, 'c' => { 'inhalt' => 33 }, 'd' => { 'inhalt' => 4 } }; h2 : $VAR1 = { 'a' => { 'inhalt' => 1 }, 'b' => { 'inhalt' => 2 }, 'c' => { 'inhalt' => 33 }, 'd' => { 'inhalt' => 4 } }; Adresse h3 -> HASH(0x1b0e4d8) Adresse h3_a -> HASH(0x15d5230) h1 : $VAR1 = { 'a' => { 'inhalt' => 1 }, 'b' => { 'inhalt' => 2 }, 'c' => { 'inhalt' => 33 }, 'd' => { 'inhalt' => 44 } }; h3 : $VAR1 = { 'a' => { 'inhalt' => 1 }, 'b' => { 'inhalt' => 2 }, 'c' => { 'inhalt' => 33 }, 'd' => { 'inhalt' => 44 } }; Adresse h4 -> HASH(0x15d5464) Adresse h4_a -> HASH(0x1b7a570) h1 : $VAR1 = { 'a' => { 'inhalt' => 1 }, 'b' => { 'inhalt' => 2 }, 'c' => { 'inhalt' => 33 }, 'd' => { 'inhalt' => 44 } }; h4 : $VAR1 = { 'a' => { 'inhalt' => 11 }, 'b' => { 'inhalt' => 2 }, 'c' => { 'inhalt' => 33 }, 'd' => { 'inhalt' => 44 } };
Wie man sieht, sind zwar die Adressen der Hashes (bzw. der Referenzen) verschiedene, sie verweisen aber alle (bis auf das letzte) auf die gleiche Substrukturen (die Substruktur zu a liegt z.B. immer an Speicherstelle 0x15d5230), deshalb wird diese auch gleich bei allen mitgeändert.
Nur das wirklich geclonte Objekt hat auch geclonte Substrukturen. Und nicht nur in Tiefe 2, sondern auch in Tiefe 123, wenn es sein muss.
Ich hoffe, das war jetzt einigermaßen verständlich.\n\n
<!--EDIT|Crian|1109865486-->
s--Pevna-;s.([a-z]).chr((ord($1)-84)%26+97).gee; s^([A-Z])^chr((ord($1)-52)%26+65)^gee;print;
use strict; use warnings; Link zu meiner Perlseite
View full thread Array of hashs
|