-1

我在 Perl:Input 5 mobile set details (Invoice number, Model, Company, Memory, Price, Quantity)中有问题。打印所有详细信息、总金额(数量 * 价格)、总数量、每家公司的手机数量。

我的脚本是:

#!/usr/bin/perl

my %mobset = (
    '1' => {
        'Invoice No' =>'3456',
        'Model'      =>'S9900',
        'Company'    =>'Sonyericcson',
        'Memory'     =>'1GB',
        'Price'      =>'8000',
        'Qnty'       =>'1',
    },
    '2' => {
        'Invoice No' => '3457',
        'Model'      => 'S3322',
        'Company'    => 'Samsung',
        'Memory'     => '2GB',
        'Price'      => '9000',
        'Qnty'       => '2',
    },
    '3' => {
        'Invoice No' => '3458',
        'Model'      => 'N4140',
        'Company'    => 'Nokia',
        'Memory'     => '512MB',
        'Price'      => '5000',
        'Qnty'       => '2',
    },
    '4' => {
        'Invoice No' => '3459',
        'Model'      => 'S4636',
        'Company'    => 'Samsung',
        'Memory'     => '256MB',
        'Price'      => '6000',
        'Qnty'       => '1',
    }, 
    '5' => {
        'Invoice No' => '3460',
        'Model'      => 'S7854',
        'Company'    => 'Samsung',
        'Memory'     => '128MB',
        'Price'      => '7000',
        'Qnty'       => '1',
    }
);

print "All the mobile set details are as follows:\n";
foreach my $id(sort keys %mobset) {

    print "Mobile SlNo. = $id, Invoice No. = $mobset{$id}{'Invoice No'}, Model No. = $mobset{$id}{'Model'}, CompanyName = $mobset{$id}{'Company'}, Memory = $mobset{$id}{'Memory'}, Price = $mobset{$id}{'Price'}, Quantity = $mobset{$id}{'Qnty'}\n";

    $totqty += $mobset{$id}{'Qnty'};
    $totprice += $mobset{$id}{'Price'};
}

print "Total Quantity of mobile set is $totqty\n";
$totamt = $totqty * $totprice;
print "Total Amount of mobile set is Rs.$totamt\n";

print "The company names of mobile are:\n";
foreach my $id(sort keys %mobset) {

    print "$mobset{$id}{'Company'}\n"; 
}

$name = SonyericcsonSamsungNokiaSamsungSamsung;
my @names = ($name =~ m/([A-Z][a-z]+)/g);
join(',',@names);
my %count;

foreach (@names) {

    if (exists $count{$_}) {

        $count{$_}++;
    } 
    else {

        $count{$_} = 1;
    }
}

print "The number of mobiles from each company are:\n";
foreach (keys %count)  {

    print "$_ \t = $count{$_}\n";
}

我的输出是:

All the mobile set details are as follows:
Mobile SlNo. = 1, Invoice No. = 3456, Model No. = S9900, CompanyName = Sonyericcson, Memory = 1GB, Price = 8000, Quantity = 1
Mobile SlNo. = 2, Invoice No. = 3457, Model No. = S3322, CompanyName = Samsung, Memory = 2GB, Price = 9000, Quantity = 2
Mobile SlNo. = 3, Invoice No. = 3458, Model No. = N4140, CompanyName = Nokia, Memory = 512MB, Price = 5000, Quantity = 2
Mobile SlNo. = 4, Invoice No. = 3459, Model No. = S4636, CompanyName = Samsung, Memory = 256MB, Price = 6000, Quantity = 1
Mobile SlNo. = 5, Invoice No. = 3460, Model No. = S7854, CompanyName = Samsung, Memory = 128MB, Price = 7000, Quantity = 1
Total Quantity of mobile set is 7
Total Amount of mobile set is Rs.245000
The company names of mobile are:
Sonyericcson
Samsung
Nokia
Samsung
Samsung
The number of mobiles from each company are:
Sonyericcson = 1
Nokia = 1
Samsung = 3

但是我得到了手机数量的 o/p,因为我确实硬编码了我不应该的手机名称。我该如何解决代码?

4

3 回答 3

0

将收集计数的代码片段更改为:

my %count;
foreach my $key ( %mobset ) {
   my $name = $mobset{$key}{Company} ;
   $count{$name}++ ;
}

或者,更好的是,将其包含在上一个循环中:

print "The company names of mobile are:\n";
foreach my $id(sort keys %mobset) {

    my $name = $mobset{$id}{Company}; 
    print "$name\n" ;
    $count{$name}++
}

注意

  1. {Company}数组键中的引号可以去掉,所以可以写成{'Company'}, 和
  2. 增加一个未定义的哈希元素是可以的(它将被视为0),因此无需检查该元素是否存在。
于 2012-10-18T08:06:37.860 回答
0

手机名称是您之前在foreach my $id(sort keys %mobset)循环中打印的名称,不是吗?您可以执行另一个类似的循环,也可以在该循环期间将名称保存在数组中并使用它。

于 2012-10-18T08:07:17.133 回答
0

真的不需要硬代码。你知道这个名字 - 它在你正在行走的 hasref 中。只需根据公司价值建立一个计数数组

my %count;
foreach my $id (sort keys %mobset) {
    ++$count{$mobset{$id}->{Company}};
}

print "The number of mobiles from each company are:\n";
foreach (keys %count)  {

    print "$_ \t = $count{$_}\n";
}
于 2012-10-18T08:09:45.093 回答