在 Perl 中分割字符串 [英] Cutting apart string in Perl

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

问题描述

我在 Perl 中有一个 23 位长的字符串.我需要把它分成不同的部分.一个变量中的前 2 个数字,另一个变量中的下 3 个数字,另一个变量中的下一个 4 个数字,等等.基本上这 23 个数字需要作为 6 个单独的变量 (2,3,4,4,3,7) 字符结束,即命令.

I have a string in Perl that is 23 digits long. I need to cut it apart into different pieces. First 2 digits in one variable, next 3 in another variable, next 4 into another variable, etc. Basically the 23 digits needs to end up as 6 separate variables (2,3,4,4,3,7) characters, in that order.

有什么想法可以像这样剪断绳子吗?

Any ideas how I can cut the string up like this?

推荐答案

有很多方法可以做到,但最短的可能是 解压:

There are lots of ways to do it, but the shortest is probably unpack:

my $string = '1' x 23;
my @values = unpack 'A2A3A4A4A3A7', $string;

如果需要单独的变量,可以使用列表赋值:

If you need separate variables, you can use a list assignment:

my ($v1, $v2, $v3, $v4, $v5, $v6) = unpack 'A2A3A4A4A3A7', $string;

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

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