+ -
当前位置:首页 → 问答吧 → Is there a function that works in opposite way to chop/chomp?

Is there a function that works in opposite way to chop/chomp?

时间:2011-07-07

来源:互联网

Hi,

I just want to append newline character to each element of an array. Is there an existing function to use?

作者: nhw_cs   发布时间: 2011-07-07

join

作者: wind_ch   发布时间: 2011-07-07

use 5.010 and say for @your_array;

作者: x9x9   发布时间: 2011-07-07



QUOTE:
join
wind_ch 发表于 2011-07-07 14:47




    join is functionally opposite to split!

作者: nhw_cs   发布时间: 2011-07-07

本帖最后由 yinyuemi 于 2011-07-07 15:00 编辑

回复 nhw_cs


  @arr= ( join "\n", map {$_} @arr,"")

作者: yinyuemi   发布时间: 2011-07-07



QUOTE:
use 5.010 and say for @your_array;
x9x9 发表于 2011-07-07 14:50




    What? Explain further please? Your answer makes little sense to me.

作者: nhw_cs   发布时间: 2011-07-07



QUOTE:
Hi,

I just want to append newline character to each element of an array. Is there an existing fun ...
nhw_cs 发表于 2011-07-07 14:42




just add the "\n" when you want output or assign to another one
print "$_\n" foreach(@array);

join also can do that
print join "\n",@array;
(note: it didn't add "\n" in last element)

作者: jason680   发布时间: 2011-07-07



QUOTE:
回复  nhw_cs


  @arr= ( join "\n", map {$_} @arr,"")
yinyuemi 发表于 2011-07-07 14:51




    It doesn't work either this way. The newline is not added to the last element !

作者: nhw_cs   发布时间: 2011-07-07

本帖最后由 yinyuemi 于 2011-07-07 15:04 编辑

回复 nhw_cs


    我已经做了修改,please try again

   @arr=(join "\n", @arr,"")

作者: yinyuemi   发布时间: 2011-07-07



QUOTE:
What? Explain further please? Your answer makes little sense to me.
nhw_cs 发表于 2011-07-07 14:52

  1. use 5.010;
  2. say for @your_array;
复制代码

作者: x9x9   发布时间: 2011-07-07