如何使用Perl合并重叠元素?

时间:2009-10-24 05:57:16

标签: perl

我已经学会了如何使用以下代码删除Perl中的重复项:

my %seen = ();
my @unique = grep { ! $seen{ $_}++ } @array;

但是,如果我想合并重叠的部分呢?有没有像上面代码那样直接完成工作的简单方法?

例如,输入文件的一些内容如下所示:

Anais Nin   :  People living deeply have no fear of death.
Pascal      :  Wisdome sends us back to our childhood.
Nietzsche   :  No one lies so boldly as the man who is indignant. 
Camus       :  Stupidity has a knack of getting its way. 
Plato       :  A good decision is based on knowledge and not on numbers. 
Anais Nin   :  We don't see things as they are, we see them as we are. 
Erich Fromm     :  Creativity requires the courage to let go of certainties. 
M. Scott Peck   :  Share our similarities, celebrate our differences.
Freud       :  The ego is not master in its own house. 
Camus       :  You cannot create experience. You must undergo it. 
Stendhal    :  Pleasure is often spoiled by describing it. 

欲望输出如下:

Anais Nin   :  People living deeply have no fear of death. We don't see things as they are, we see them as we are. 
Pascal      :  Wisdome sends us back to our childhood.
Nietzsche   :  No one lies so boldly as the man who is indignant. 
Camus       :  Stupidity has a knack of getting its way.  You cannot create experience. You must undergo it. 
Plato       :  A good decision is based on knowledge and not on numbers. 
Erich Fromm     :  Creativity requires the courage to let go of certainties. 
M. Scott Peck   :  Share our similarities, celebrate our differences.
Freud       :  The ego is not master in its own house. 
Stendhal    :  Pleasure is often spoiled by describing it. 

一如既往地感谢任何指导!

4 个答案:

答案 0 :(得分:7)

这是正则表达式和哈希的一个非常简单的应用。我将您的数据放入名为“merge.txt”的文件中。这会将结果打印到标准输出。

#! perl
use warnings;
use strict;
open my $input, "<", "merge.txt" or die $!;
my %name2quotes;
while (my $line = <$input>) {
    if ($line =~ /(.*?)\s*:\s*(.*?)\s*$/) {
        my $name = $1;
        my $quote = $2;
        if ($name2quotes{$name}) {
            $name2quotes{$name} .= " " . $quote;
        } else {
            $name2quotes{$name} = $quote;
        }
    } # You might want to put an "else" here to check for errors.
}
close $input or die $!;
for my $name (sort keys %name2quotes) {
    print "$name : $name2quotes{$name}\n";
}

答案 1 :(得分:3)

您可以在不测试哈希元素是否存在的情况下连接引用。如果哈希元素不存在,它将自动生成哈希元素。

my %lib;
for (<DATA>){
    chomp;
    my ($au, $qu) = split /\s+:\s+/, $_, 2;
    $lib{$au} .= ' ' . $qu;
}

print $_, " : ", $lib{$_}, "\n" for sort keys %lib;

__DATA__
# Not shown.

答案 2 :(得分:2)

while (<>) {
    ($F1,$F2) = split(/[:\n]/, $_);
    $F1 =~ s/[[:space:]]+//g;
    if (!(defined $a{$F1})) {
        $a{$F1} = $F2;
    }
    else {
        $a{$F1} = "$a{$F1} $F2";
    }
}
foreach $i (keys %a) {
    print $i, $a{$i} . "\n";
}

输出

 $ perl test.pl file
    Freud  The ego is not master in its own house.
    ErichFromm  Creativity requires the courage to let go of certainties.
    Camus  Stupidity has a knack of getting its way.    You cannot create experience. You must undergo it.
    M.ScottPeck  Share our similarities, celebrate our differences.
    Plato  A good decision is based on knowledge and not on numbers.
    Pascal  Wisdome sends us back to our childhood.
    Nietzsche  No one lies so boldly as the man who is indignant.
    AnaisNin  People living deeply have no fear of death.   We don't see things as they are, we see them as we are.
    Stendhal  Pleasure is often spoiled by describing it.

答案 3 :(得分:1)

我刚刚浏览了其他与Perl相关的帖子和​​帖子,并发现Schwern对标题为“How do I load a file into a Perl hash?”的问题的回答实际上可以解决我的问题。看起来不同的人可能会以完全不同的方式表达同一个问题。

通过一些必要的修改和添加打印哈希指令,我想出了以下工作代码:

#!perl
use warnings;
use autodie;
use strict;

open my $quotes,'<','c:/quotes.txt';
my %hash;
while (<$quotes>)
{
   chomp;
   my ($au, $qu) = split /\s+:\s+/, $_, 2;
   $hash{$au} .= exists $hash{$au}? "$qu" : $qu;

}
print map { "$_ : $hash{$_}\n" } keys %hash;
相关问题