2012-04-14 110 views
2

我想知道是否有人可以用perl來幫助絕望的新手以下問題。我一整天都在努力,但是我的perl書在工作中,我似乎無法在谷歌裏找到任何相關的東西......或許我真的很愚蠢。從製表符分隔的文本文件列製作數組

我有一個看起來像一個文件中:

Bob  April 
Bob  April 
Bob  March 
Mary August 
Robin December 
Robin April 

輸出文件我後:

Bob  April April March 
Mary August 
Robin December April 

因此,它列出了每個月的順序,它似乎爲每個人。

我試着把它變成一個散列,但當然它不會讓我有重複,所以我想我想每個名稱都有數組(在這個例子中,鮑勃,瑪麗和羅賓)。 我害怕上傳我一直試圖調整的代碼,因爲我知道這將是可怕的錯誤。我想我需要定義(?)數組。這是正確的嗎?

任何幫助將不勝感激,我保證我將在此期間更多地研究perl。

謝謝你的時間,耐心和幫助。

#!/usr/bin/perl -w 

while (<>) { 
    chomp; 
    if (defined $old_name) { 
     $name=$1; 
     $month=$2; 
     if ($name eq $old_name) { 
      $array{$month}++; 
      } 
     else { 
      print "$old_name"; 
      foreach (@array) { 
       push (@array, $month); 
       print "\[email protected]"; 
       } 
      print "\n"; 
      @array=(); 
      $array{$month}++; 
      } 
     } 
    else { 
     $name=$1; 
     $month=$2; 
     $array{month}++; 
     } 
    $old_name=$name; 
    } 
print "$old_name"; 
foreach (@array) { 
    push (@array, $month); 
    print "\[email protected]"; 
    } 
print "\n"; 

回答

2

對於這麼簡單的任務,你的代碼看起來過於複雜。

use strict; 
use warnings; 

my %hash; 
while (<DATA>) { 
    my ($name, $mon) = split; 
    push @{$hash{$name}}, $mon; 
} 

for my $name (keys %hash) { 
    my @months = @{$hash{$name}}; 
    print "$name\[email protected]\n"; 
}  
__DATA__ 
Bob  April 
Bob  April 
Bob  March 
Mary August 
Robin December 
Robin April 
1

您有點接近。你確實希望使用一個以名字爲關鍵字的散列,但正如你所看到的,對於你想要存儲的每個名字數組,因此你希望使用的數據結構是數組散列(或者更確切地說數組引用,因爲這是在Perl中實現的)

雖然在此,請不要養成使用全局變量的習慣 - 您的代碼的100%在開始時應該有「use strict; use warnings;」,並且在本地範圍內(my )變量。

use strict; 
my %data; 
my @sorted_names; # Only needed if you care which order to print the results 
while (<>) { 
    chomp; 
    my ($name, $month) = split(/s+/); 
    if (! $data{$name}) { 
     # Initialize to empty array reference if first time. 
     # Not required - perl will do it for you 
     $data{$name} ||= []; 
     # Only needed if you want to print results in the same order of names as input. 
     push @sorted_names, $name; 
    } 
    push @{ $data{$name} }, $month; 
} 

foreach my $name (@sorted_names) { 
    print "$name\t" . join(" ", @{ $data{$name} }) . "\n"; 
} 
# If don't care about name order, just do "foreach my $name (keys %data) {" 
+0

你並不需要初始化標量值爲(空)的數組引用,它是自動完成的。 – TLP 2012-04-14 15:23:14

+1

@TLP - 是真的,但對於一個新手來說,這有點不直觀。 – DVK 2012-04-14 15:24:48

1

腳本:

#!/usr/bin/perl 
use strict; 
use warnings; 

my %content; 
open my $fh, '<file.txt' or die $!; 
while (<$fh>) { 
    push @{$content{$1}}, $2 if /^(\S+)\s+(\S+)\s*$/; 
} 
close $fh; 
foreach (keys %content) { 
    print $_, "\t"; 
    foreach my $item (@{$content{$_}}) { 
    print "$item "; 
    } 
    print "\n"; 
} 

#!/usr/bin/perl 
use strict; 
use warnings; 

my %content; 
open my $fh, '<file.txt' or die $!; 
while (<$fh>) { 
    push @{$content{$1}}, $2 if /^(\S+)\s+(\S+)\s*$/; 
} 
close $fh; 
print "$_\[email protected]{$content{$_}}\n" for keys %content; 

#!/usr/bin/perl 
use strict; 
use warnings; 

my %content; 
open my $fh, '<file.txt' or die $!; 
s/^(\S+)\s+(\S+)\s*$/{push @{$content{$1}}, $2}/e for <$fh>; 
close $fh; 
print "$_\[email protected]{$content{$_}}\n" for keys %content; 

輸出:

Bob  April April March 
Mary August 
Robin December April 

文件file.txt與內容:

Bob  April 
Bob  April 
Bob  March 
Mary August 
Robin December 
Robin April 
相關問題