我正在尝试使用哈希引用将信息传递给子例程。 Psuedo代码:
sub output_detail {
Here I want to be able to access each record by the key name (ex. "first", "second", etc)
}
sub output_records {
I want to use a foreach to pass each record has reference to another sub-routine
that handles each record.
foreach $key ( sort( keys %someting) ) {
output_detail(something);
}
}
%records = ();
while ($recnum, $first, $second, $third) = db_read($handle)) {
my %rec = ("first"=>$first, "second"=>$second, "third=>$third);
my $id = $recnum;
$records{$id} = \%rec;
}
output_records(\%records);
我不确定在传递给子程序时如何取消引用哈希值。 任何想法都会非常有用。
由于
答案 0 :(得分:1)
使用->
访问哈希引用的键。因此,您对output_records的参数将作为标量散列引用。
sub output_records {
my $records = shift;
my $first = $records->{"first"};
}
有关详细信息,请参阅perlreftut。
答案 1 :(得分:0)
sub output_detail {
my $hash = shift;
my $value = $$hash{some_key};
}
sub output_records {
my $hash = shift;
foreach my $key (sort keys %$hash) {
output_detail($hash, $key);
# or just pass `$$hash{$key}` if you only need the value
}
}