2017-02-19 48 views
0

我試圖完成我在另一種編程語言中完成的任務,但Perl看起來有點不同,而且我很掙扎。參數不是數字

我需要建立一個基本的數據庫,這個數據庫應該根據它們的第一個字母來保存字符串。

例如,當我給這個輸入:SQL AHT NFK IOS C64 SQL AHT END

所需的輸出將是:

A – E: AHT C64 
F – J: IOS 
K – O: NFK 
P – T: SQL 
U – Z: 

但是,當我給,例如,AAA BBB最終,我得到噸的錯誤和所有他們是非常相似的:

Argument "AAA" isn't numeric in addition (+) at proje.pl line 76 <STDIN> line 3. 

我把所有的代碼,因爲我不知道這個錯誤的原因是什麼。我已經檢查了其他問題,但我沒有太多幫助。

use warnings; 

    sub doesExistInDatabase { 

    my ($abbreviation) = @_; 
    my @database = @_; 
    my $boolean = 0;  

    for(my $c = 1; $c < 20; $c++){ 
     for(my $d = 0; $d < 5; $d++){ 
      if($database[$c][$d] eq ($abbreviation)){ 
       $boolean = 1; 
      } 
     } 
    } 
    return $boolean; 
} 

    $database[0][0] = "A-E"; 
    $database[0][1] = "F-J"; 
    $database[0][2] = "K-O"; 
    $database[0][3] = "P-T"; 
    $database[0][4] = "U-Z"; 

    for(my $i = 1; $i < 20; $i++){ 
     for(my $k = 0; $k < 5; $k++){ 
      $database[$i][$k] = ""; 
     } 
    } 

    @numberOfElements = (0,0,0,0,0);  

     while($numberOfElements[0] < 20 and $numberOfElements[1] < 20 and $numberOfElements[2] < 20 
      and $numberOfElements[3] < 20 and $numberOfElements[4] < 20){ 

      my $abbreviation = <STDIN>; 
      chomp($abbreviation); 
      my @chars = split //, $abbreviation; 

      my $existing = doesExistInDatabase($abbreviation, @database); 


      if($abbreviation eq "END"){ 
       last; 
      } 

      if($existing == 0){ 
       if($chars[0] eq "A" or $chars[0] eq "B" or $chars[0] eq "C" or 
         $chars[0] eq "D" or $chars[0] eq "E"){ 
        $numberOfElements[0]++; 
        $database[$numberOfElements[0]][0] = $abbreviation; 
       } 
       if($chars[0] eq "F" or $chars[0] eq "G" or $chars[0] eq "H" or 
         $chars[0] eq "I" or $chars[0] eq "J"){ 
        $numberOfElements[1]++; 
        $database[$numberOfElements[1]][0] = $abbreviation; 
       } 
       if($chars[0] eq "K" or $chars[0] eq "L" or $chars[0] eq "M" or 
         $chars[0] eq "N" or $chars[0] eq "O"){ 
        $numberOfElements[2]++; 
        $database[$numberOfElements[2]][0] = $abbreviation; 
       } 
       if($chars[0] eq "P" or $chars[0] eq "Q" or $chars[0] eq "R" or 
         $chars[0] eq "S" or $chars[0] eq "T"){ 
        $numberOfElements[3]++; 
        $database[$numberOfElements[3]][0] = $abbreviation; 
       } 
       if($chars[0] eq "U" or $chars[0] eq "V" or $chars[0] eq "W" or 
         $chars[0] eq "X" or $chars[0] eq "Y" or $chars[0] eq "Z"){ 
        $numberOfElements[4]++; 
        $database[$numberOfElements[4]][0] = $abbreviation; 
       } 
      } 
    } 
    print("\n$database[0][0]: "); 
    for(my $x = 1; $x < $numberOfElements[0]+1; $x++){ 
     printf(" " + $database[$x][0]); 
    } 
    print("\n$database[0][1]: "); 
    for(my $x = 1; $x < $numberOfElements[0]+1; $x++){ 
     printf(" " + $database[$x][1]); 
    } 
    print("\n$database[0][2]: "); 
    for(my $x = 1; $x < $numberOfElements[0]+1; $x++){ 
     printf(" " + $database[$x][2]); 
    } 
    print("\n$database[0][3]: "); 
    for(my $x = 1; $x < $numberOfElements[0]+1; $x++){ 
     printf(" " + $database[$x][3]); 
    } 
    print("\n$database[0][4]: "); 
    for(my $x = 1; $x < $numberOfElements[0]+1; $x++){ 
     printf(" " + $database[$x][4]); 
    } 
+4

你應該開始用'用嚴格的全Perl的文件;使用警告;'自動查找許多常見問題。 – melpomene

+1

不要在'print'足夠的地方使用'printf'。特別是,你需要有一個非常好的理由來使用帶有非常量格式字符串的'printf'。 – melpomene

+1

「+」運算符添加兩個數字。 – melpomene

回答

2

你可以讓你的代碼了很多更具可讀性和避免這些多維數組和C風格的循環在同一時間。一個簡單的stringwise比較是所有你需要爲了你的條件篩選到水桶:

use strict; 
use warnings; 

use Data::Dump; 

sub bucketize { 
    my %buckets = (
     'A-E' => {}, 
     'F-J' => {}, 
     'K-O' => {}, 
     'P-T' => {}, 
     'U-Z' => {}, 
    ); 

    for my $term (@_) {  
     for my $bucket (reverse(sort(keys(%buckets)))) { 
      if ($term gt $bucket) { 
       $buckets{$bucket}{$term}++; 
       last; 
      } 
     } 
    } 

    return \%buckets; 
} 

my $hash_ref = bucketize(qw(SQL AHT NFK IOS C64 SQL AHT)); 
dd($hash_ref); 

輸出:

{ 
    "A-E" => { AHT => 2, C64 => 1 }, 
    "F-J" => { IOS => 1 }, 
    "K-O" => { NFK => 1 }, 
    "P-T" => { SQL => 2 }, 
    "U-Z" => {}, 
} 
0

在perl中,字符串連接的運算符是。 (期)不+(加),你已經使用:

printf(" " + $database[$x][0]) 

應該是:

printf(" " . $database[$x][0]) 

或者只是:

print " $database[$x][0]" 

當你有Perl的更多的知識,也許你我想看看這個解決方案:

my(@group,%group) = qw(A-E F-J K-O P-T U-Z); 
while(<>){ 
    my $abbr=$_; chomp($abbr); 
    $group{$_}{$abbr}++ for grep $abbr=~/^[$_]/i, @group; 
} 
print "$_: ".join(" ",sort keys %{$group{$_}})."\n" for @group; 

測試:

for i in SQL AHT NFK IOS C64 SQL AHT END AAA BBB END PERL;do echo $i;done|perl program.pl 
A-E: AAA AHT BBB C64 END 
F-J: IOS 
K-O: NFK 
P-T: PERL SQL 
U-Z: 
相關問題