简体   繁体   English

join with @ 打印错误的结果

[英]join with @ prints the wrong result

I would like to know how can I use the sign @ together with join operator;我想知道如何将符号@join运算符一起使用; this yields a wrong result:这会产生错误的结果:

my $string1  = '00aabb';
my $string2  = '02babe';
print join ("A",@ $string1, $string2);

Namely:即:

$ perl b.pl
02babe

So how can I use @ within join that would concatenate the two strings and joins them with a separator A ?那么如何在join中使用@来连接两个字符串并用分隔符A连接它们? I come out of the assumption that join requires an array as its second argument, and it thus should begin with @ .我的假设是join需要一个数组作为它的第二个参数,因此它应该以@开头。 The mere仅仅是

print join("A", $string1, $string2);

works fine, but I want to make it work with @ preceded.工作正常,但我想让它与@前面一起工作。

join does not require an array as its second argument; join不需要数组作为它的第二个参数; it requires a list (which is different from an array):它需要一个列表(与数组不同):

join EXPR,LIST加入EXPR,LIST

Your 2nd code example works fine because the 2nd and 3rd arguments make up a list of 2 items.您的第二个代码示例工作正常,因为第二个和第三个 arguments 构成了 2 个项目的列表。 Here is a way to use join if you already have an array variable ( @strings ):如果您已经有一个数组变量( @strings ),这是一种使用join的方法:

use warnings;
use strict;

my $string1  = '00aabb';
my $string2  = '02babe';

print join ("A", $string1, $string2);
print "\n";

my @strings = ($string1, $string2);

print join ("A", @strings);
print "\n";

Prints:印刷:

00aabbA02babe
00aabbA02babe

The @ sign is what is used to denote an array variable. @符号用于表示数组变量。

See also perldoc perldata .另请参阅perldoc perldata


Note that if you had used strict , you would have gotten an error message.请注意,如果您使用了strict ,您将收到一条错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM