如何在Perl中将字符串分割成两个字符的块?

时间:2022-01-10 02:14:38

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

如何在Perl中提取一个字符串,并将它分割成一个数组,每个数组有两个字符长?

I attempted this:

我尝试:

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

but did not get the expected results.

但是没有得到预期的结果。

Ultimately I want to turn something like this

最终我想把它变成这样

F53CBBA476

in to an array containing

到包含的数组中

F5 3C BB A4 76

4 个解决方案

#1


53  

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

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中的列表上下文中以一种特殊的方式运行。它迭代地处理操作,将模式与前面匹配后的其余文本匹配。然后,从每个模式匹配应用程序中匹配的所有文本组成列表。

#2


37  

If you really must use split, you can do a :

如果你真的必须使用split,你可以做a:

grep {length > 0} split(/(..)/, $string);

But I think the fastest way would be with unpack :

但我认为最快的方法是打开行李:

unpack("(A2)*", $string);

Both these methods have the "advantage" that if the string has an odd number of characters, it will output the last one on it's own.

这两种方法都有一个“优点”,即如果字符串有奇数个字符,那么它将自己输出最后一个字符。

#3


6  

Actually, to catch the odd character, you want to make the second character optional:

实际上,要捕获奇数字符,您需要使第二个字符可选:

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

#4


5  

The pattern passed to split identifies what separates that which you want. If you wanted to use split, you'd use something like

传递给split的模式标识您想要的分离。如果你想使用split,你可以使用类似的方法

my @pairs = split /(?(?{ pos() % 2 })(?!))/, $string;

or

my @pairs = split /(?=(?:.{2})+\z)/s, $string;

Those are rather poor solutions. Better solutions include:

这些都是很糟糕的解决方案。更好的解决方案包括:

my @pairs = $string =~ /..?/sg;  # Accepts odd-length strings.

my @pairs = $string =~ /../sg;

my @pairs = unpack '(a2)*', $string;

#1


53  

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

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中的列表上下文中以一种特殊的方式运行。它迭代地处理操作,将模式与前面匹配后的其余文本匹配。然后,从每个模式匹配应用程序中匹配的所有文本组成列表。

#2


37  

If you really must use split, you can do a :

如果你真的必须使用split,你可以做a:

grep {length > 0} split(/(..)/, $string);

But I think the fastest way would be with unpack :

但我认为最快的方法是打开行李:

unpack("(A2)*", $string);

Both these methods have the "advantage" that if the string has an odd number of characters, it will output the last one on it's own.

这两种方法都有一个“优点”,即如果字符串有奇数个字符,那么它将自己输出最后一个字符。

#3


6  

Actually, to catch the odd character, you want to make the second character optional:

实际上,要捕获奇数字符,您需要使第二个字符可选:

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

#4


5  

The pattern passed to split identifies what separates that which you want. If you wanted to use split, you'd use something like

传递给split的模式标识您想要的分离。如果你想使用split,你可以使用类似的方法

my @pairs = split /(?(?{ pos() % 2 })(?!))/, $string;

or

my @pairs = split /(?=(?:.{2})+\z)/s, $string;

Those are rather poor solutions. Better solutions include:

这些都是很糟糕的解决方案。更好的解决方案包括:

my @pairs = $string =~ /..?/sg;  # Accepts odd-length strings.

my @pairs = $string =~ /../sg;

my @pairs = unpack '(a2)*', $string;