Leser: 3
4 Einträge, 1 Seite |
pktm+2008-05-20 09:52:41--Wie mache ich das, dass $p per Referenz modifiziert wird?
1
2
3
4
5
6
sub x {
my $p = shift;
for( my $i = 0; $i < 42; $i++ ) {
$p->[$i]->{'soekey'} = 'some_value';
}
}
1
2
3
4
5
6
sub x {
my $p = shift;
foreach my $x ( @{$p} ) {
$$x{'soekey'} = 'some_value';
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = ( { test => 1, test2 => 3},{ test => 1, test2 => 3}, { test => 1, test2 => 3},{ test => 1, test2 => 3} ); print Dumper \@array; x(\@array); print Dumper \@array; sub x{ my $p = shift; for my $i ( 0..3 ){ $p->[$i]->{test2} = 'etwas anderes'; } }
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
C:\>perl test.pl
$VAR1 = [
{
'test' => 1,
'test2' => 3
},
{
'test' => 1,
'test2' => 3
},
{
'test' => 1,
'test2' => 3
},
{
'test' => 1,
'test2' => 3
}
];
$VAR1 = [
{
'test' => 1,
'test2' => 'etwas anderes'
},
{
'test' => 1,
'test2' => 'etwas anderes'
},
{
'test' => 1,
'test2' => 'etwas anderes'
},
{
'test' => 1,
'test2' => 'etwas anderes'
}
];
4 Einträge, 1 Seite |