删除重复的哈希值并在perl中保留它的计数

时间:2009-06-09 15:50:36

标签: perl hash

让我们说

%hash = (
        key1 => 'one',
        key2 => 'two',
        key3 => 'three',
        key4 => 'two',
        key5 => 'one',
);

我想要两个数组:

  1. 此数组应具有唯一键/值

    @ array1 =(key1 one key2 two key3 three)

  2. 这个数组应该按值重复计数(例如,这里只有三个值是唯一的,因此'one'被发现两次,'two'被发现两次,'three'被找到一次)。

    @ array2 =(2 2 1)

3 个答案:

答案 0 :(得分:2)

忽略订单问题......

#!/usr/bin/perl

use strict;
use warnings;

my %hash = qw( key1 one key2 two key3 three key4 two key5 one );

my %counts;
$counts{ $_ }++ for values %hash;

my %uniq = reverse %hash;

my (@array1, @array2);

while ( my ($k, $v) = each %uniq ) {
    push @array1, $v, $k;
    push @array2, $counts{$k};
}

use Data::Dumper;
print Dumper \@array1, \@array2;


__END__

C:\Temp> t.pl
$VAR1 = [
          'key3',
          'three',
          'key5',
          'one',
          'key2',
          'two'
        ];
$VAR2 = [
          1,
          2,
          2
        ];

答案 1 :(得分:1)

我不确定为什么你希望最终的答案是那种奇怪的形式(而不是哈希的唯一键/值和哈希值=>计数映射,但是你去吧......

您也可以直接使用%count散列,而不是将值推送到数组中。

use strict; 
use Data::Dumper;

my %hash = ( key1 => "one", key2 => "two", key3 => "three", key4 => "two", key5 => "one" );
my %counts = ();
my @counts = ();
my %unique = ();
foreach my $key (sort keys %hash) {
    my $value = $hash{$key}; 
    if (not exists $counts{$value}) {
        $unique{$key} = $value;
    }
    $counts{$value}++;
};
my @unique_keys = sort keys %unique; # Fix the sorting to your desired one 
                                     # if default is not what you meant

# You can also use %counts hash directly 
#instead of pushing values into an array as you wanted above.
foreach my $key (@unique_keys) {
    push @counts, $counts{ $unique{$key} }
};

# Print
print Data::Dumper->Dump([\@unique_keys, \@counts, \%unique],  
                         ["unique_keys", "counts", "unique"] ) ; 

<强> 结果:

$unique_keys = [
                 'key1',
                 'key2',
                 'key3'
               ];
$counts = [
            2,
            2,
            1
          ];
$unique = {
            'key2' => 'two',
            'key1' => 'one',
            'key3' => 'three'
          };

答案 2 :(得分:0)

my %hash = (
        key1 => 'one',
        key2 => 'two',
        key3 => 'three',
        key4 => 'two',
        key5 => 'one',
);

my %counts;

$counts{$_}++ foreach values %hash;

my @elements = keys %hashes;

my @frequency = values %hashes;