質問1:どのようにして配列をPerlの関数に渡すことができますか?
私は関数に配列を渡したいと思います。しかし、渡された引数は関数内で変更されます。それは価値によって呼び出されますか?
質問2:
#my ($name, $num, @array)= @_; <=1)
my $name = shift; <=2)
my $num = shift;
my @array = shift;
ケース1と2は、異なる出力を持っています。なぜそれが起こったのですか?以下は
#!/usr/bin/perl
use strict;
my @test1;
push @test1, ['a', 1];
push @test1, ['b', 1];
push @test1, ['c', 1];
push @test1, ['d', 1];
push @test1, ['e', 1];
for (my $i=0; $i< scalar(@test1); $i++) {
print "out1: $test1[$i][0] $test1[$i][1]\n";
}
test_func("test_func", 10, @test1);
sub test_func {
#my ($name, $num, @array)= @_; <=1)
my $name = shift; <=2)
my $num = shift;
my @array = shift;
print "$name\n";
print "$num\n";
for (my $i=0; $i< scalar(@test1); $i++) {
print "$array[$i][0] $array[$i][1]\n";
}
for (my $i=0; $i< scalar(@test1); $i++) {
if ($array[$i][0] eq 'a') {
$array[$i][0] = 'z';
}
}
for (my $i=0; $i< scalar(@test1); $i++) {
print "change: $array[$i][0] $array[$i][1]\n";
}
}
for (my $i=0; $i< scalar(@test1); $i++) {
print "out2: $test1[$i][0] $test1[$i][1]\n";
}
#
テスト出力です。
out1: a 1
out1: b 1
out1: c 1
out1: d 1
out1: e 1
test_func
10
a 1
b 1
c 1
d 1
e 1
change: z 1
change: b 1
change: c 1
change: d 1
change: e 1
out2: z 1 <= Why did it change?
out2: b 1
out2: c 1
out2: d 1
out2: e 1
種類の説明をお願い致します。私の困難な問題を解決します^^ – user1395438
この解決策が助けになった場合は、その隣にあるチェックマークを –
にしてください。もし見つからなければ、何が欠けているか教えてください。 – ikegami