Leser: 6
4 Einträge, 1 Seite |
1
2
3
4
5
6
@a=(0,1,2,3,4,5,{ich=>'ich'});
print "a= @a, $a[6]{ich} \n";
@d=@a;
$d[6]{ich} = 'du';
$d[7]=8;
print "d= @d, $d[6]{ich} \na= @a, $a[6]{ich}"
1
2
3
a= 0 1 2 3 4 5 HASH(0x224f00), ich
d= 0 1 2 3 4 5 HASH(0x224f00) 8, du
a= 0 1 2 3 4 5 HASH(0x224f00), du
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/perl
use strict;
use warnings;
use Clone qw(clone);
use Data::Dumper;
my @a=(0,1,2,3,4,5,{ich=>'ich'});
print Dumper(\@a);
my @d=@{clone(\@a)};
$d[6]{ich} = 'du';
$d[7]=8;
print Dumper(\@a,\@d);
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 Data::Dumper;
sub deep_clone($);
my @a=(0,1,2,3,4,5,{ich=> [1,2,3]});
print Dumper(\@a);
my @d=@{deep_clone(\@a)};
$d[6]{ich} = 'du';
$d[7]=8;
print Dumper(\@a,\@d);
sub deep_clone($){
my ($ref) = @_;
my $target;
unless(ref($ref)){
$target = $ref;
}
elsif(ref($ref) eq 'ARRAY'){
for my $idx(0..scalar(@$ref)-1){
$target->[$idx] = deep_clone($ref->[$idx]);
}
}
elsif(ref($ref) eq 'HASH'){
for my $key(keys %$ref){
$target->{$key} = deep_clone($ref->{$key});
}
}
return $target;
}
4 Einträge, 1 Seite |