小编goz*_*ozu的帖子

对于我是否将标量或数组传递给perl中的子例程感到困惑?

我刚刚开始学习perl,我对此练习感到困惑(来自Learning Perl第4章).

在greet()子例程的开头,我试图将参数$ _分配给我的$ name(我的$ name = $ _)变量,但它不起作用.这本书说要用"我的名字=班次"; 但我不明白为什么.shift用于从数组中删除一个值,据我所知,我的参数不是一个数组,它是一个标量内的字符串!

谁能解释一下我不理解的东西?

谢谢!这是整个代码.

use 5.012;
use warnings;
use utf8;

sub greet {
   my $name = $_;
   state $last_person ;
   if (defined $last_person ) {
          print "Hi $name! $last_person is also here!\n";

   } else {
          print "Hi $name! You are the first one here!\n";
   }
   $last_person = $name;
}

greet( 'Fred' );
greet( 'Barney' );
greet( 'Wilma' );
greet( 'Betty' );
Run Code Online (Sandbox Code Playgroud)

arrays perl scalar shift subroutine

4
推荐指数
1
解决办法
200
查看次数

Ruby koan 182 Greed骰子游戏 - 得到一个神秘的错误

我正在做边缘小屋学习红宝石,而我却被贪婪的koan(182-183)卡住了一个神秘的错误.这里列出了规则

我知道我的代码是......令人印象深刻,我想我一旦我的逻辑声音(它可能不是)就重构它.

我感谢任何帮助.

def score(dice)
  score = 0
  if dice == []
    return score
  end

  dice = dice.sort
  dice = [1,1,4,5,6]
  count = [0,0,0,0,0,0]
  score = 0
  dice.each do |face|
    if    face == 1
        count[0]++
    elsif face == 2 # this is line 45 with reported error
        count[1]++
    elsif face == 3
        count[2]++
    elsif face == 4
        count[3]++
    elsif face == 5
        count[4]++
    elsif face == 6
        count[5]++
    end
  end
  if count[0] >= 3
    score+= 1000
    count[0] = …
Run Code Online (Sandbox Code Playgroud)

ruby

0
推荐指数
1
解决办法
556
查看次数

标签 统计

arrays ×1

perl ×1

ruby ×1

scalar ×1

shift ×1

subroutine ×1