如何在 Perl 中将字符串拆分为两个字符的块? [英] How can I split a string into chunks of two characters each in Perl?

查看:22
本文介绍了如何在 Perl 中将字符串拆分为两个字符的块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Perl 中获取一个字符串并将其拆分为一个数组,每个条目的长度为两个字符?

How do I take a string in Perl and split it up into an array with entries two characters long each?

我尝试过:

@array = split(/../, $string);

但没有得到预期的结果.

but did not get the expected results.

最终我想变成这样

F53CBBA476

放入一个包含

F5 3C BB A4 76

推荐答案

@array = ( $string =~ m/../g );

模式匹配运算符在 Perl 的列表上下文中以一种特殊的方式运行.它迭代地处理操作,将模式与前一次匹配之后的文本的其余部分进行匹配.然后列表由在模式匹配的每个应用期间匹配的所有文本形成.

The pattern-matching operator behaves in a special way in a list context in Perl. It processes the operation iteratively, matching the pattern against the remainder of the text after the previous match. Then the list is formed from all the text that matched during each application of the pattern-matching.

这篇关于如何在 Perl 中将字符串拆分为两个字符的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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