以前に定義したハッシュに要素 (値を持つキー) を追加するサブルーチンを作成したいと考えています。このサブルーチンはループ内で呼び出されるため、ハッシュが大きくなります。返されるハッシュで既存の要素を上書きしたくありません。
最後に、蓄積されたハッシュ全体を出力したいと思います。
現在、何も印刷されていません。最終的なハッシュは空に見えますが、そうではありません。ハッシュ参照で試してみましたが、実際には機能しません。短い形式で、私のコードは次のようになります。
sub main{
my %hash;
%hash=("hello"=>1); # entry for testing
my $counter=0;
while($counter>5){
my(@var, $hash)=analyse($one, $two, \%hash);
print ref($hash);
# try to dereference the returning hash reference,
# but the error msg says: its not an reference ...
# in my file this is line 82
%hash=%{$hash};
$counter++;
}
# here trying to print the final hash
print "hash:", map { "$_ => $hash{$_}\n" } keys %hash;
}
sub analyse{
my $one=shift;
my $two=shift;
my %hash=%{shift @_};
my @array; # gets filled some where here and will be returned later
# adding elements to %hash here as in
$hash{"j"} = 2; #used for testing if it works
# test here whether the key already exists or
# otherwise add it to the hash
return (@array, \%hash);
}
しかし、これはまったく機能しません。サブルーチンanalyse
はハッシュを受け取りますが、返されたハッシュ参照が空であるか、わかりません。結局何も印刷されませんでした。
最初は、それは参照ではないと言いましたが、今は次のように言っています:
未定義の値を HASH 参照として使用できません C:/Users/workspace/Perl_projekt/Extractor.pm 82 行目。
私の間違いはどこですか?
アドバイスをいただければ幸いです。