我该如何动态执行以下代码

时间:2016-11-03 07:57:47

标签: perl

循环可能会进行迭代10.我的意思是目前它有五个循环,它可能会达到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";
        }
    }

就像我需要1到10美元一样。

3 个答案:

答案 0 :(得分:4)

阅读perlsub以及List::Util,因为这些将简化您的大部分代码。我还建议始终 use strictuse 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";
    }
}

答案 1 :(得分:4)

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返回所有非零元素。在标量上下文(和unless的布尔上下文)中 - 返回所有非零元素的数字。
  • 来自List :: util包的
  • 函数sum()返回数组元素的总和

答案 2 :(得分:0)

让我们看看我们是否可以将它们放在一起

代码的开头看起来不错

encrypt()

现在缺少3行:

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";

最后的2个助手潜艇;:

if (is_0_hash($count, %hashj)) {
   print sum_hash($count, %hashh);
}

HTH 乔治