循环可能会上升到迭代10,我的意思是目前它有5个循环,它可能会上升到10个循环。我想重写脚本来简化它。
use strict;
my %hashj=(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0);
my %hashh=(1 => 10, 2 => 18 ,3 => 35, 4 => 40, 5 => 42);
use POSIX;
my $count=ceil(rand(int(4)));
print $count."\n";
if($count==2)
{
if($hashj{$count-1}==0 && $hashj{$count}==0)
{
print $hashh{$count-1} + $hashh{$count},"\n";
}
}
if($count==3)
{
if($hashj{$count-2}==0 && $hashj{$count-1}==0 && $hashj{$count}==0)
{
print $hashh{$count-2} + $hashh{$count-1} + $hashh{$count},"\n";
}
}
if($count==4)
{
if($hashj{$count-3}==0 && $hashj{$count-2}==0 && $hashj{$count-1}==0 && $hashj{$count}==0)
{
print $hashh{$count-3} + $hashh{$count-2} + $hashh{$count-1} + $hashh{$count},"\n";
}
}
if($count==5)
{
if($hashj{$count-4}==0 && $hashj{$count-3}==0 && $hashj{$count-2}==0 && $hashj{$count-1}==0 && $hashj{$count}==0)
{
print $hashh{$count-4} + $hashh{$count-3} + $hashh{$count-2} + $hashh{$count-1} + $hashh{$count},"\n";
}
}期望的输出:它应该像函数一样,如果我将$count值设置为5,那么它必须生成以下代码。
if($count==5)
{
if($hashj{$count-4}==0 && $hashj{$count-3}==0 && $hashj{$count-2}==0 && $hashj{$count-1}==0 && $hashj{$count}==0)
{
print $hashh{$count-4} + $hashh{$count-3} + $hashh{$count-2} + $hashh{$count-1} + $hashh{$count},"\n";
}
}就像我需要的$count 1到10。
发布于 2016-11-03 16:27:16
请阅读perlsub和List::Util,因为它们将简化您的大部分代码。我还建议always use strict和use warnings,以及尝试使用更有意义的变量名。以下是代码的简化:
use strict;
use warnings;
use List::Util qw( all sum );
my %hashj = ( 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0 );
my %hashh = ( 1 => 10, 2 => 18, 3 => 35, 4 => 40, 5 => 42 );
my @hash_keys = keys %hashj;
my $count = $hash_keys[ int rand scalar @hash_keys ]; # Random element
print $count."\n";
check_hashes( \%hashj, \%hashh, $count); # \%foo is a reference to hash %foo
sub check_hashes {
my ($hash_1, $hash_2, $count) = @_;
if ($count < 2) {
return; # you seem not to care about this case...
}
my @indexes = grep { $_ <= $count } keys %$hash_1; # Dereference a hash reference by putting % in front
if ( all { $hash_1->{$_} == 0 } @indexes ) {
my $total = sum map { $hash_2->{$_} } @indexes;
print $total . "\n";
}
}发布于 2016-11-03 17:07:29
use strict;
use List::Util qw(sum);
my %hashj=(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0);
my %hashh=(1 => 10, 2 => 18 ,3 => 35, 4 => 40, 5 => 42);
use POSIX;
my $count=ceil(rand(int(4)));
print $count."\n";
print sum @hashh{1..$count} unless $count<=1 || grep {$_} @hashj{1..$count};1..$count返回1到$count.@hash{ 1, 2, 3 }之间的所有数字=值的数组($hash{1}, $hash{2}, $hash{3}).grep {$_} for array返回所有非零元素。In标量上下文(以及unless的布尔上下文)-返回number所有非零列表中的sum()::util包返回数组元素的总和发布于 2016-11-03 16:21:14
让我们看看我们能不能把这件事做好
代码的开头看起来不错
use strict;
my %hashj=(1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0);
my %hashh=(1 => 10, 2 => 18 ,3 => 35, 4 => 40, 5 => 42);
use POSIX;
my $count=ceil(rand(int(4)));
print $count."\n";现在缺少3行:
if (is_0_hash($count, %hashj)) {
print sum_hash($count, %hashh);
}末尾的2个辅助subs;:
sub is_0_hash {
my ($count, %hash) = @_;
foreach my $i ( 1 .. $count) {
if ($hash{$i} != 0) {
return #false
}
}
return 1;
}
sub sum_hash {
my ($count, %hash) = @_;
my $sum = 0;
foreach my $i ( 1 .. $count) {
$sum += $hash{$i};
}
return $sum;
}HTH格奥尔格
https://stackoverflow.com/questions/40396388
复制相似问题