简体   繁体   English

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

[英]How do I find, count, and display unique elements of an array using Perl?

I am a novice Perl programmer and would like some help. 我是Perl的新手程序员,希望获得一些帮助。 I have an array list that I am trying to split each element based on the pipe into two scalar elements. 我有一个数组列表,我试图将基于管道的每个元素分成两个标量元素。 From there I would like to spike out only the lines that read 'PJ RER Apts to Share' as the first element. 从这里开始,我只想摘录显示“ PJ RER Apts to Share”作为第一要素的行。 Then I want to print out the second element only once while counting each time the element appears. 然后,我只想打印第二个元素一次,同时计数每次元素出现的时间。 I wrote the piece of code below but can't figure out where I am going wrong. 我在下面编写了这段代码,但无法弄清楚我要去哪里。 It might be something small that I am just overlooking. 我可能忽略的地方可能很小。 Any help would be greatly appreciated. 任何帮助将不胜感激。

## 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  

This is very crude, but I'd use Perl's awesome hash arrays to help with this task. 这非常粗糙,但是我会使用Perl的超棒哈希数组来帮助完成此任务。 I'd take the entire record and use it to index the hash array and an increment to the value. 我会记录整个记录,并用它来索引哈希数组和该值的增量。

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

When it's done, cycle through your hash array and you'll have unique and duplicate records alike counted from the increment counter. 完成后,循环遍历您的哈希数组,您将拥有从增量计数器开始计数的唯一和重复记录。

Like I said this is very crude and I'm sure there are many issues with the approach. 就像我说的那样,这很粗糙,我敢肯定这种方法存在很多问题。 All ye Perl gods fire away. 你们所有的Perl众神都开除了。

You can use the uniq function in List::MoreUtils to remove duplicate entries from a list. 您可以在List :: MoreUtils中使用uniq函数从列表中删除重复的条目。 The number of elements in a list or array can be easily found by evaluating the list in scalar context: 通过在标量上下文中评估列表,可以轻松找到列表或数组中元素的数量:

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

I used the following script: 我使用以下脚本:

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";
   }
}

And it yielded: 它产生了:

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

Is this what you were looking for? 这是您要找的东西吗?

Accumulate the number of occurrence per city in a hash. 累积哈希中每个城市的出现次数。 The key will be the city name and the value will be the count. 密钥将是城市名称,值将是计数。 Then sort the keys and output them and their corresponding values: 然后对键排序并输出它们及其对应的值:

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"; 

Also, did you want a count of all listings or just those you want to match. 另外,您是否要统计所有列表或仅想匹配的列表。 If it is the later change the next to the last line to: 如果是更高版本,则将最后一行旁边的内容更改为:

$count = keys %apts;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM