问题描述
这是我的代码。在这个过程结束时,我希望按升序来收集名称和总和结果,并且想要打印。我尝试使用as @ sum1和@ sum2,但它不知道@sum函数。如何做到这一点或任何想法将是非常宝贵的。
this my code.At the end of this process I want to collect name and sum results with ascending order and want to print.I tried to take the as @sum1 and @sum2 but it couldn't know @sum functions.How can I do that or any idea will be very valuable.
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw( sum );
use Data::Dumper qw(Dumper);
my %grades;
$grades{"Ahmet"}{quiz1} = 97;
$grades{"Ahmet"}{quiz2} = 67;
$grades{"Ahmet"}{quiz3} = 93;
$grades{"Mehmet"}{quiz1} = 88;
$grades{"Mehmet"}{quiz2} = 82;
$grades{"Mehmet"}{quiz3} = 99;
print Dumper \%grades;
print "----------------\n";
foreach my $name ( sort keys %grades ) {
my %hash1 = (
'Ahmet' => [ 97, 67, 93 ],
'Mehmet' => [ 88, 82, 99 ],
);
my @sums;
for my $key ( keys %hash1 ) {
my $sum = sum @{ $hash1{$key} };
push @sums, "$key: $sum\n";
}
foreach my $sum ( keys %{ $grades{$name} } ) {
print "$name : $grades{$name}{$sum}\n";
}
print @sums;
}
foreach my $name ( sort keys %grades ) {
print "$grades{$name}\n";
}
my %grades2;
$grades2{"Merve"}{quiz1} = 33;
$grades2{"Merve"}{quiz2} = 41;
$grades2{"Merve"}{quiz3} = 59;
$grades2{"Aslı"}{quiz1} = 79;
$grades2{"Aslı"}{quiz2} = 31;
$grades2{"Aslı"}{quiz3} = 62;
print Dumper \%grades2;
print "----------------\n";
foreach my $name2 ( sort keys %grades2 ) {
my %hash = (
'Merve' => [ 33, 41, 59 ],
'Aslı' => [ 79, 31, 62 ],
);
my @sums2;
for my $key ( keys %hash ) {
my $sum = sum @{ $hash{$key} };
push @sums2, "$key: $sum\n";
}
foreach my $sum ( keys %{ $grades2{$name2} } ) {
print "$name2 : $grades2{$name2}{$sum}\n";
}
print @sums2;
}
foreach my $name2 ( sort keys %grades2 ) {
print "$grades2{$name2}\n";
}
my %info;
$info{$_} .= "A" for keys %grades;
$info{$_} .= "B" for keys %grades2;
for ( sort keys %info ) {
print "$_ : $info{$_}\n";
}
推荐答案
在你的代码中。一旦你填充了%的等级,就不需要创建%散列 - 它可以直接从%等级中提取。
You seem to duplicate some information in your code. Once you populate the %grades, there's no need to create the %hash - it can be extracted from the %grades directly.
要填充散列,你不需要必须一直重复键,并描述如下所示的子结构。
To populate the hashes, you don't have to repeat the keys all the time, describe the substructure as shown below.
另外,您的代码包含ı,它不是ASCII字符。通过使用并声明输出的编码来告诉Perl会更好。
Also, your code contains "ı", which isn't an ASCII character. It would be nicer to tell Perl that by using utf8 and also declaring the encoding of the output.
要按总和对输出进行排序,请使用排序函数与自定义块比较散列元素:
To sort the output by sums, use the sort function with a custom block that compares the hash elements:
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use open ':encoding(UTF-8)', ':std';
use List::Util qw( sum );
my %grades = (
Ahmet => {
quiz1 => 97,
quiz2 => 67,
quiz3 => 93,
},
Mehmet => {
quiz1 => 88,
quiz2 => 82,
quiz3 => 99,
}
);
my %sum;
for my $name (keys %grades)
{
$sum{$name} = sum(values %{ $grades{$name} });
}
for my $name (sort { $sum{$a} <=> $sum{$b} } keys %sum)
{
print $name, ' ', $sum{$name}, "\n";
}
my %grades2 = (
Merve => {
quiz1 => 33,
quiz2 => 41,
quiz3 => 59,
},
Aslı => {
quiz1 => 79,
quiz2 => 31,
quiz3 => 62,
},
);
my %sum2;
for my $name (keys %grades2)
{
$sum2{$name} = sum(values %{ $grades2{$name} });
}
for my $name (sort { $sum2{$a} <=> $sum2{$b} } keys %sum2)
{
print $name, ' ', $sum2{$name}, "\n";
}
这篇关于二维散列数据输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!