Perl分割每n个字符和新行

时间:2014-01-25 18:27:49

标签: perl

我是perl的新手。我知道我可以通过unpack或使用正则表达式来分割一些恒定数量的字符。 但有没有一些标准的方法来分割每n个字符和新行?

这是我要分割的字符串:

my $str="hello\nworld";
my $num_split_chars=2;

1 个答案:

答案 0 :(得分:3)

也许以下内容会有所帮助:

use strict;
use warnings;
use Data::Dumper;

my $str             = "hello\nworld";
my $num_split_chars = 2;
$num_split_chars--;

my @arr = $str =~ /.{$num_split_chars}.?/g;
print Dumper \@arr;

输出:

$VAR1 = [
          'he',
          'll',
          'o',
          'wo',
          'rl',
          'd'
        ];