2013-03-06 65 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); 
相关问题