perl如何用split分割字符串并自动在末尾加上换行符?

来源:百度知道 编辑:UC知道 时间:2024/05/13 02:50:23
如题,例如在 split(//, "hello world")这样,但需要再"h"后还自动添加上换行符……

不知道你想把加上换行后的值保存在什么样类型的变量里。

1.结果保存在标量里:
my $str = "hello world";
my $rst = join "\n",split(/ /, $str);
print "$rst";
输出结果:
hello
world

2.结果保存在列表里
my $str = "hello world";
my @rst = map { $_."\n" } split(/ /, $str);
print "$rst[0]";
print "$rst[1]";
输出结果:
hello
world