2013-03-06 60 views
0

我有一個這樣的文件:如何分割每個空白的數組的每個條目?

This is is my "test" 
file with a lot 
words separeted by whitespace. 

現在我想達到這樣我創建一個數組,其中每個元素都包含一個字,所有重複的單詞被刪除

所需陣列拆分此:

This 
is 
my 
test 
etc... 

我將文件讀入數組,但我不知道如何拆分整個數組,以便結果是一個新的數組。我怎樣才能刪除重複的單詞?

#!/usr/bin/perl 
package catalogs; 
use Log::Log4perl; 
Log::Log4perl->init("log4perl.properties"); 


open(FILE, "<Source.txt") || die "file Sources.txt konnte nicht geoeffnet werden"; 

my @fileContent = <FILE>; 
close FILE; 

my $log = Log::Log4perl->get_logger("catalogs"); 

@fileContent = split(" "); 

回答

2

要提取的話,你可以使用

my @words = $str =~ /\w+/g; 

至於刪除重複,

use List::MoreUtils qw(uniq); 
my @uniq_words = uniq @words; 

my %seen; 
my @uniq_words = grep !$seen{$_}++, @words; 
+0

你能解釋一下你提取方法嗎?什麼是$ str?我已經將文件讀入數組名稱@fileContent,是不是指這個數組而不是$ str? – 2013-03-06 23:49:08

+0

您想從中提取單詞的字符串。數組不包含單詞。 – ikegami 2013-03-06 23:54:26

+0

...如果您想要搜索數組中的所有字符串,請執行此操作。或者不要首先使用你不需要的數組。 – ikegami 2013-03-07 04:43:08

0

你加載的文本文件放到一個數組中,但它可能會做更多感覺將文件加載到單個字符串中。這將使您能夠利用提供的解決方案@ikegami。要將它們放在一起,請嘗試以下操作。

use List::MoreUtils qw(uniq); 
my $filecontent = do 
{ 
    local $/ = undef; 
    <STDIN>; 
}; 
my @words = $filecontent =~ /\w+/g; 
my @uniqword = uniq(@words); 
0
my $log = Log::Log4perl->get_logger("catalogs"); 
@fileContent = split(/\s+/, $log); 
@filecontent = uniq(@filecontent); 

讓文字獨特的,你可以使用uniq子程序或將其映射到hash。由於散列鍵總是唯一的,所以重寫將被覆蓋。

use strict; 
use warnings; 
use Data::Dumper; 

my @a = (1,1,1,2,3,4,4); 
my %hash =(); 
%hash = map $_=>'1', @a; 
my @new = keys(%hash); 
print Dumper(@new); 
相關問題