0
讓一個perl腳本從xml讀取,將數據解析爲一個子哈希,輸出哈希數組,然後從主調用第二個子數組來處理數組的哈希。Perl:如何訪問傳遞給子例程的哈希數組
Data :: Dumper顯示一切正常通過。
有一個可怕的時間搞清楚爲什麼我現在不能訪問哈希。
use strict;
use warnings;
use Data::Dumper;
my (@sortedData, $value1, $value2);
use subs qw(processData outputData);
@sortedData = processData;
outputData($value1, $value2, \@sortedData);
sub processData{
# Example code
# # Does some processing of xml that results in a hash.
# # That series of hashes is pushed onto an array
my ($item, @results);
# foreach $item (@{ $rss->{items}){
# my %data = (
# 'first' => $item->{'value'},
# 'second' => $item->{'value'},
# 'third' => $item->{'value'}
# );
# push @results, \%data;
# }
# Essentially the hash is :
@results = (
{'data1'=>810,'data2'=>153,'data3'=>215},
{'data1'=>160,'data2'=>220,'data3'=>604},
{'data1'=>940,'data2'=>103,'data3'=>115},
{'data1'=>100,'data2'=>319,'data3',525},
{'data1'=>500,'data2'=>803,'data3'=>650}
);
return @results;
}
sub outputData{
my ($input1, $input2, @localData) = @_;
print Dumper @localData;
print "\@localData: " . @localData . "\n";
foreach my $i (@localData){
# foreach my $j ($i){
# print $i . "\n" . $j . "\n";
# }
print "\$i: " . $i . "\n";
}
}
輸出:
$VAR1 = [
{
'data3' => 215,
'data2' => 153,
'data1' => 810
},
{
'data3' => 604,
'data2' => 220,
'data1' => 160
},
{
'data2' => 103,
'data3' => 115,
'data1' => 940
},
{
'data1' => 100,
'data3' => 525,
'data2' => 319
},
{
'data1' => 500,
'data3' => 650,
'data2' => 803
}
];
@localData: 1
$i: ARRAY(0x80071b30)
我不理解爲什麼@localData的值是1或爲什麼@localData的元件的參考是數組,而不是HASH。
我當然是第一次測試我對PERL數組的更深入的理解。 我不像我想的那麼瞭解它。
您將*引用*傳遞給數組(標量)並將其分配給'@ localData',將引用放置在'$ localData [0]'中。 – ikegami