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

时间:2021-01-11 07:28:38

I am a novice Perl programmer and would like some help. 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. 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.

我是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 个解决方案

#1


2  

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?

这是你在寻找什么?

#2


3  

This is very crude, but I'd use Perl's awesome hash arrays to help with this task. I'd take the entire record and use it to index the hash array and an increment to the value.

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

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神都开火了。

#3


3  

You can use the uniq function in List::MoreUtils to remove duplicate entries from a list. The number of elements in a list or array can be easily found by evaluating the list in scalar context:

您可以使用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

#4


0  

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;

#1


2  

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?

这是你在寻找什么?

#2


3  

This is very crude, but I'd use Perl's awesome hash arrays to help with this task. I'd take the entire record and use it to index the hash array and an increment to the value.

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

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神都开火了。

#3


3  

You can use the uniq function in List::MoreUtils to remove duplicate entries from a list. The number of elements in a list or array can be easily found by evaluating the list in scalar context:

您可以使用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

#4


0  

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;