简体   繁体   English

Perl的。 比较具有重复值的数组

[英]Perl. Compare arrays with repeated values

I have two arrays of strings with repeated values that I would like to compare and get the number of add/del/upd(=same) elements: 我有两个具有重复值的字符串数组,我想比较它们并获取add / del / upd(= same)元素的数量:

my @array1 = ("aaa", "bbb", "ccc", "eee", "eee");
my @array2 = ("aaa", "aaa", "bbb", "ccc", "ccc", "ddd", "fff");

I need something like: 我需要类似的东西:

add: 4
del: 2
upd: 3

I tried List::Compare : 我试过List :: Compare

my @array1 = ("aaa", "bbb", "ccc", "eee", "eee");
my @array2 = ("aaa", "aaa", "bbb", "ccc", "ccc", "ddd", "fff");

my $lc = List::Compare->new(\@array1, \@array2);        
print Dumper "intersection (upd): ".scalar($lc->get_intersection);
print Dumper "only first   (del): ".scalar($lc->get_unique);
print Dumper "only second  (add): ".scalar($lc->get_complement);

But it cannot work for repeated values: 但是它不能用于重复的值:

$VAR1 = 'intersection (upd): 3';
$VAR1 = 'only first   (del): 1';
$VAR1 = 'only second  (add): 2';

How can I solve this problem? 我怎么解决这个问题?

This will do what you need. 这将满足您的需求。

I trust the mechanism is clear. 我相信机制很明确。 If you need further explanation please ask again. 如果您需要进一步的说明,请再次询问。

use strict;
use warnings;

my @array1 = qw( aaa bbb ccc eee eee );
my @array2 = qw( aaa aaa bbb ccc ccc ddd fff );

my %data;

$data{$_}[0]++ for @array1;
$data{$_}[1]++ for @array2;

my ($add, $del, $upd) = (0, 0, 0);

for (values %data) {
  my ($a1, $a2) = map $_ // 0, @$_[0,1];
  if ($a1 < $a2) {
    $upd += $a1;
    $add += $a2 - $a1;
  }
  else {
    $upd += $a2;
    $del += $a1 - $a2;
  }
}

printf "add/del/upd = %d/%d/%d\n", $add, $del, $upd;

output 产量

add/del/upd = 4/2/3

You're working with multisets (aka bags) (elements can occur multiple times), not sets (elements are unique). 您正在使用多集合 (也称为袋)(元素可以多次出现),而不是集合(元素是唯一的)。 Use Set::Bag . 使用Set :: Bag

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

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