【问题标题】:How to concatenate variables in Perl如何在 Perl 中连接变量
【发布时间】:2012-07-31 17:13:46
【问题描述】:

在 Perl 中连接变量有不同的方法吗?

我不小心写了下面这行代码:

print "$linenumber is: \n" . $linenumber;

这导致输出如下:

22 is:
22

我期待:

$linenumber is:
22

然后我想知道。它必须将双引号中的$linenumber 解释为对变量的引用(太酷了!)。

使用这种方法有哪些注意事项以及它是如何工作的?

【问题讨论】:

  • 从技术上讲,您不需要连接变量,因为print 可以接受多个参数。 print '$linenumber is: \n', $linenumber; 也可以。 (尽管您需要注意内置变量$,(请参阅man perlvar)。
  • '$linenumber is: \n' 不会做你想做的:-)

标签: string perl concatenation string-concatenation


【解决方案1】:

在制定此回复时,我发现this webpage 解释了以下信息:

###################################################
#Note that when you have double quoted strings, you don't always need to concatenate. Observe this sample:

#!/usr/bin/perl

$a='Big ';
$b='Macs';
print 'I like to eat ' . $a . $b;

#This prints out:
#  I like to eat Big Macs

###################################################

#If we had used double quotes, we could have accomplished the same thing like this:

#!/usr/bin/perl

$a='Big ';
$b='Macs';
print "I like to eat $a $b";

#Printing this:
#  I like to eat Big Macs
#without having to use the concatenating operator (.).

###################################################

#Remember that single quotes do not interpret, so had you tried that method with single quotes, like this:


#!/usr/bin/perl

$a='Big ';
$b='Macs';
print 'I like to eat $a $b';
#Your result would have been:
#  I like to eat $a $b
#Which don't taste anywhere near as good.

我认为这会对社区有所帮助,所以我提出这个问题并回答我自己的问题。非常欢迎其他有用的答案!

【讨论】:

    【解决方案2】:

    您可以使用反斜杠 $ 以原样打印:

    print "\$linenumber is: \n" . $linenumber;
    

    这会打印出您所期望的内容。如果您不希望 Perl 插入变量名,也可以使用单引号,但 "\n" 将按字面意思插入。

    【讨论】:

    • 这是一个很好的观点。有件事我忘了提。认为这不完全是我的问题。无论如何 +1。
    • 使用单引号的问题(如您所建议的)是它会从字面上解释换行符,请参阅我的编辑。
    【解决方案3】:

    使用双引号时会发生变量插值。所以,特殊字符需要转义。在这种情况下,您需要转义$

    print "\$linenumber is: \n" . $linenumber;
    

    可以改写为:

    print "\$linenumber is: \n$linenumber";
    

    为避免字符串插值,请使用单引号:

    print '$linenumber is: ' . "\n$linenumber";  # No need to escape `$`
    

    【讨论】:

    • 当插值变量且变量名边界不清晰时(如"my $page = 12; print "$pagep."),需要将变量名用大括号${...}括起来。 IE。 print "${page}p."
    【解决方案4】:

    在 Perl 中,任何用双引号构建的字符串都将被插入,因此任何变量都将被其值替换。像许多其他语言一样,如果您需要打印 $,您将不得不对其进行转义。

    print "\$linenumber is:\n$linenumber";
    

    print "\$linenumber is:\n" . $linenumber;
    

    printf "\$linenumber is:\n%s", $linenumber;
    

    Scalar Interpolation

    【讨论】:

      【解决方案5】:

      如果您从

      更改代码
      print "$linenumber is: \n" . $linenumber;
      

      print '$linenumber is:' . "\n" . $linenumber;
      

      print '$linenumber is:' . "\n$linenumber";
      

      它会打印出来

      $linenumber is:
      22
      

      当想要打印变量名时,我发现有用的是使用单引号,这样其中的变量就不会被翻译成它们的值,从而使代码更易于阅读。

      【讨论】:

        【解决方案6】:

        我喜欢.=运算符方法:

        #!/usr/bin/perl
        use strict;
        use warnings;
        
        my $text .= "... contents ..."; # Append contents to the end of variable $text.
        $text .= $text; # Append variable $text contents to variable $text contents.
        print $text; # Prints "... contents ...... contents ..."
        

        【讨论】:

          猜你喜欢
          • 2019-06-17
          • 1970-01-01
          • 1970-01-01
          • 2011-05-14
          • 1970-01-01
          • 1970-01-01
          • 2023-03-20
          • 2015-07-27
          • 1970-01-01
          相关资源
          最近更新 更多