用线数组标 [英] Scalar with lines to array

查看:113
本文介绍了用线数组标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么可以转换含有换行符的字符串标量数组中的这些行作为元素呢?

How can I convert a scalar containing a string with newlines in an array with those lines as elements?

例如,考虑这样的:

$lines = "line 1\nline 2\nline 3\n";

我想取出这个:

$lines[0] --> "line 1\n"
$lines[1] --> "line 2\n"
$lines[2] --> "line 3\n"

在理想情况下,我想保持换行符在阿雷的元素。

Ideally, I'd like to keep the newline in the aray elements.

推荐答案

使用拆分

my @lines = split(/\n/m, $lines);

修改:保持换行符,分上 / ^ / M 在评论中提到,或使用零宽度look-背后,作为另一个答复中提到。

EDIT: to keep the newlines, split on /^/m as mentioned in the comments, or use a zero-width look-behind, as mentioned in another answer.

这篇关于用线数组标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆