2012-07-19 67 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

這是輕微的通用::導杆模塊的文檔不清楚。作爲一個用戶,你不關心它是一個匿名數組。您關心的是它是對匿名數組的引用。

當然,你只能通過引用訪問一個匿名數組,但有時候人們並沒有意識到這一點。

當我在我的培訓課程中介紹參考資料時,我總是向人們展示一個人的樣子。並告訴他們,他們不需要知道這一點,但在某些時候,他們會意外地打印引用變量時意外打印引用 - 所以能夠識別引用變量是有用的。

相關問題