2012-07-19 64 views
2

正如您从下面的Perl代码片段可以看到的,我将$document字符串(其中包含来自文本文档的文本)放入@document阵列中。然后在打印出来之前打印出$document。然后我阻止@document阵列,然后将阻塞结果放入我的$stemmed_words_anon_array字符串中,但我得到:ARRAY(0xc99b3c)这就像存储器地址。为什么我的Perl代码导致我的字符串接收ARRAY(0xc99b3c)而不是字符串的内容?

我在做什么错?我的results_stemmed.txt也包含里面的ARRAY(0xc99b3c)

# Put string of main document into an array 
my @document = split(' ', $document); 

# Print the $document string to check it before stemming it 
print $document; 

open (FILE_STEM, '>results_stemmed.txt'); 
use Lingua::Stem qw(stem); 
my $stemmed_words_anon_array = stem(@document); 
# $stemmed_words_anon_array is just receiving: ARRAY(0xcbacb) here 
print FILE_STEM $stemmed_words_anon_array; 
close(FILE_STEM); 
print $stemmed_words_anon_array; 

回答

5

这是一个参考。 @$stemmed_words_anon_array会让你阵列本身。有关如何处理Perl中的引用的更多信息,请参阅perldoc perlref

1

您可以使用File::Slurp::write_file快速编写的@$stemmed_words_anon_array的全部内容:

use File::Slurp qw(write_file); 
use Lingua::Stem qw(stem); 

my $stemmed_words = stem(split ' ', $document); 
write_file 'results_stemmed.txt', $stemmed_words; 
print "@$stemmed_words\n"; 
1

这是轻微的通用::导杆模块的文档不清楚。作为一个用户,你不关心它是一个匿名数组。您关心的是它是对匿名数组的引用。

当然,你只能通过引用访问一个匿名数组,但有时候人们并没有意识到这一点。

当我在我的培训课程中介绍参考资料时,我总是向人们展示一个人的样子。并告诉他们,他们不需要知道这一点,但在某些时候,他们会意外地打印引用变量时意外打印引用 - 所以能够识别引用变量是有用的。

相关问题