参数不是数字的

时间:2017-02-19 21:15:06

标签: perl

我试图完成一项我用另一种编程语言完成的任务,但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 END时,我会遇到很多错误,而且所有错误都非常相似:

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]);
    }

2 个答案:

答案 0 :(得分:2)

您可以使代码更具可读性,同时避免使用多维数组和C风格循环。为了将术语筛选到存储桶中,只需进行简单的逐字比较即可:

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" => {},
}

答案 1 :(得分: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: