2009-10-24 4 views
2

다음 코드를 사용하여 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. 

언제나처럼 모든 안내에 감사드립니다!

+0

왜 downvote? 내 질문에 올바른 표현 방법은 무엇입니까? 감사. – Mike

답변

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

테스트 완료! 나를 위해, 그것은 전혀 간단하지 않습니다. 교훈 주셔서 감사합니다 :) – Mike

+2

또한'else'를'if' 다음에 추가하여 줄을 파싱하는 데 오류가 있는지 확인하는 것이 좋습니다. –

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. 
+0

@ ghostdog74, 이것도 작동합니다. 코드를 공유해 주셔서 감사합니다 :) 나는 잘 모르겠지만 "$ FS = ':'; ' 유용하지 않습니다. – Mike

3

당신은 해시 요소의 존재에 대한 시험없이 견적을 연결할 수 있습니다. Perl은 아직 존재하지 않으면 해시 요소를 자동으로 활성화합니다.

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. 
+0

와우,이 코드는 정말 인상적입니다. 공유 주셔서 감사합니다, FM! – Mike

1

난 그냥 SO의 다른 펄 관련 게시물이 스레드를 통해 참조한 "How do I load a file into a Perl hash?"실제로 내 문제를 해결할 수라는 질문에 Schwern에의 해답을 발견했다. 다른 사람들이 똑같은 질문을 아주 다르게 표현할 수있는 것처럼 보입니다. 몇 가지 필요한 수정 및 인쇄 해시 지침을 첨가

, 나는 다음과 같은 작업 코드를 내놓았다 :

#!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; 
관련 문제