11. 字符串中数组内插
- 数组的内容可以被内插到双引号引起的字符串中,内插时,各个元素之间自动添加空格分隔。
- 数组内插之后首尾不会增加额外的空格,如果需要手动添加。
- 如果需要@符号,\转义或者单引号定义字符串。
- 索引表达式会被当成普通字符串表达式处理。
- 如果要在某个标量变量后面写左方括号,需要先将这个左方括号隔开。
my @rocks = qw/ flintstone slate rubble /;
print "quartz @rocks limestone\n";
print "Three rocks are: @rocks.\n";
my @bedrock = qw/ slate rubble /;
print "fred@bedrock.edu\n";
print "fred\@bedrock.edu" . "\n";
print 'fred@bedrock.edu' . "\n";
my @fred = qw( hello dolly);
my $y = 2;
my $x = "This is $fred[1]'s place.\n";
print "$x";
$x = "This is $fred[$y-1]'s place.\n";
print "$x";
my $z = 2*4;
print $z . "\n";
$x = "This is $fred[$z-1]'s place.\n";
print "$x";
@fred = qw( hello dolly);
my $fred = "right";
print "this is $fred[1].\n"; #数组索引;
print "this is ${fred}[1].\n"; #标量,用花括号避开歧义
print "this is $fred"."[1].\n"; #标量
print "this is $fred\[1].\n"; #标量
网友评论