如何使用Perl查找,计算和显示数组的唯一元素?

时间:2010-05-12 15:14:44

标签: perl arrays grep unique

我是Perl的新手程序员,想要一些帮助。我有一个数组列表,我试图将基于管道的每个元素拆分为两个标量元素。从那里开始,我想只将“PJ RER Apts to Share”作为第一个元素。然后我想在每次元素出现时计算一次打印出第二个元素。我在下面编写了这段代码,但无法弄清楚我哪里出错了。它可能是我只是俯视的小东西。任何帮助将不胜感激。

## CODE ##

my @data = ('PJ RER Apts to Share|PROVIDENCE',  
        'PJ RER Apts to Share|JOHNSTON',  
        'PJ RER Apts to Share|JOHNSTON',  
        'PJ RER Apts to Share|JOHNSTON',  
        'PJ RER Condo|WEST WARWICK',  
        'PJ RER Condo|WARWICK');  

foreach my $line (@data) {  
    $count = @data;  
    chomp($line);  
    @fields = split(/\|/,$line);  
    if (($fields[0] =~ /PJ RER Apts to Share/g)){  
        @array2 = $fields[1];  
        my %seen;  
        my @uniq = grep { ! $seen{$_}++ } @array2;  
        my $count2 = scalar(@uniq);  
        print "$array2[0] ($count2)","\n"  
    }  
}  
print "$count","\n";  

## OUTPUT ##

PROVIDENCE (1)  
JOHNSTON (1)  
JOHNSTON (1)  
JOHNSTON (1)  
6  

4 个答案:

答案 0 :(得分:3)

这非常粗糙,但我会使用Perl的超棒哈希数组来帮助完成这项任务。我将获取整个记录并使用它来索引哈希数组和值的增量。

foreach (@array) {
   $myHash{$_}++;
}

完成后,循环遍历哈希数组,您将从增量计数器中计算出唯一且重复的记录。

就像我说的那样非常粗糙,我确信这种方法存在很多问题。你们所有的Perl神都会消失。

答案 1 :(得分:3)

您可以使用List::MoreUtils中的uniq功能从列表中删除重复的条目。通过在标量上下文中评估列表,可以轻松找到列表或数组中的元素数量:

use strict; use warnings;
use List::MoreUtils 'uniq';
my @list = qw(1 1 2 3 5 8);

my @uniq = uniq @list;
print 'list with dupes removed: ', join(', ', @uniq), "\n";
print 'number of elements in this list: ', scalar(@uniq), "\n";
list with dupes removed: 1, 2, 3, 5, 8
number of elements in this list: 5

答案 2 :(得分:2)

我使用了以下脚本:

my %elements = ( );

foreach (@data) {
   chomp;
   my ($f0, $f1) = split(/\|/);
   $elements{ $f0 }{ $f1 }++;
}

while ( my ( $k, $v ) = each( %elements ) )
{
   print "Key [$k] :\n";
   while ( my ( $field2, $count ) = each( %$v ) )
   {
      print "  Field [$field2] appeared $count times\n";
   }
}

它产生了:

Key [PJ RER Condo] :
  Field [WARWICK] appeared 1 times
  Field [WEST WARWICK] appeared 1 times
Key [PJ RER Apts to Share] :
  Field [JOHNSTON] appeared 3 times
  Field [PROVIDENCE] appeared 1 times

这是你在找什么?

答案 3 :(得分:0)

累计哈希中每个城市的出现次数。键将是城市名称,值将是计数。然后对键进行排序并输出它们及其相应的值:

my @data = ('PJ RER Apts to Share|PROVIDENCE',  
    'PJ RER Apts to Share|JOHNSTON',  
    'PJ RER Apts to Share|JOHNSTON',  
    'PJ RER Apts to Share|JOHNSTON',  
    'PJ RER Condo|WEST WARWICK',  
    'PJ RER Condo|WARWICK');  

foreach my $line (@data) {   
    chomp($line);  
    @fields = split(/\|/,$line);  
    if ($fields[0] eq "PJ RER Apts to Share"){  
        $city = "\u\L$fields[1]";
        $apts{$city}++;  

    }  
} 

@city_sort = sort (@city);  
print map {"$_ $apts{$_}\n";} sort(keys %apts);  
$count = @data; 
print "$count","\n"; 

此外,您是否想要计算所有列表或只是想要匹配的列表。如果是后者,则将最后一行的下一行更改为:

$count = keys %apts;