【问题标题】:Save the result of an expression to be used in a separate block保存表达式的结果以在单独的块中使用
【发布时间】:2014-05-10 19:29:38
【问题描述】:

如您所见,我想将第一次替换的结果存储在$enkel 中。我在if 的打印中使用了这个$enkel

但是当我想在elsif 打印中使用这个$enkel 时,它没有任何价值。我实际上想在elsif 打印中同时使用$dubbel$enkel

有没有办法让 Perl 将其永久存储在 $dubbel 中,以便可以在其他打印中使用?

if ($inputwoord =~ /((aa|uu|ee|oo)[^aeiour])$/) {
  ($enkel = $inputwoord) =~ s/([aueo])\1/$1/g;
  print "$enkel$buig\n$inputwoord$gen\n$enkel$comp\n$enkel$compe\n$inputwoord$sup\n$inputwoord$supe\n";
}
elsif ($inputwoord =~ /[^aeiou][aeiou]([pktgnmlf])$/) {
  ($dubbel = $inputwoord) =~ s /([pktgnmlf]$)/$1$1/g;
  print "$dubbel$buig\n$inputwoord$gen\n$dubbel$comp\n$dubbel$compe\n$inputwoord$sup\n$inputwoord$supe\n";
} # consonantgeminatie

【问题讨论】:

  • 请格式化您的代码,以便我们理解并运行它。帮助我们为您提供帮助。
  • 请不要将 cmets(如星号)添加到代码本身。代码应该独立存在,任何评论都应该在问题的文本中。重要的是我们可以看到正在运行的代码
  • 我猜你的程序顶部没有use strictuse warnings?要么,要么你在开始时在一个块中定义你的所有变量。您必须始终拥有use strictuse warnings,并且应使用my 声明变量,使其尽可能接近它们的第一个使用点。

标签: perl variables store substitution


【解决方案1】:

如果您想在ifelsif 子句中都使用$enkel 的值,则必须在if 语句之外计算它。像这样

($enkel = $inputwoord)  =~ s/([aueo])\1/$1/g;
($dubbel = $inputwoord) =~ s/([pktgnmlf]$)/$1$1/g;

if ($inputwoord =~ /((aa|uu|ee|oo)[^aeiour])$/) {
  print "$enkel$buig\n$inputwoord$gen\n$enkel$comp\n$enkel$compe\n$inputwoord$sup\n$inputwoord$supe\n";
}
elsif ($inputwoord =~ /[^aeiou][aeiou]([pktgnmlf])$/) {
  print "$dubbel$buig\n$inputwoord$gen\n$dubbel$comp\n$dubbel$compe\n$inputwoord$sup\n$inputwoord$supe\n";
} # consonantgeminatie

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 2020-08-05
    相关资源
    最近更新 更多