我可以使用 unpack 将字符串拆分为 Perl 中的字符吗?
常见的“Perlism”正在生成一个列表,以这种形式循环:
for($str=~/./g) { print "the next character from \"$str\"=$_\n ”; 在这种情况下,全局匹配正
则表达式返回一个列表,该列表依次是字符串 $str
中的一个字符,并将该值分配给 $_
而不是正则表达式、split
可以以相同的方式使用或 'a'..'z'
、map
等。
我正在调查 < code>unpack 通过字符串的字段解释生成字段。我一直发现 unpack
对于我的大脑工作方式来说不太简单,而且我从未真正深入研究过它。
作为一个简单的情况,我想使用 unpack 生成一个列表,其中每个元素中都有一个字符(是的 - 我知道我可以使用 split(//,$str)
和/./g
但我真的想看看 unpack 是否可以这样使用...)
显然,我可以使用一个字段列表进行解包,即 unpack("A1" x length ($str), $str)
但是还有其他一些看起来像通配符的方式吗?即,我可以在列表上下文或循环中调用 unpack(some_format,$str)
,以便 unpack 将返回格式组中的下一组字符,直到 $str 耗尽为止?
我已阅读Perl 5.12 Pack pod 和 Perl 5.12 包教程 和 < a href="http://www.perlmonks.org/?node_id=224666" rel="nofollow noreferrer">Perkmonks 教程
以下是示例代码:
#!/usr/bin/perl
use warnings;
use strict;
my $str=join('',('a'..'z', 'A'..'Z')); #the alphabet...
$str=~s/(.{1,3})/$1 /g; #...in groups of three
print "str=$str\n\n";
for ($str=~/./g) {
print "regex: = $_\n";
}
for(split(//,$str)) {
print "split: \$_=$_\n";
}
for(unpack("A1" x length($str), $str)) {
print "unpack: \$_=$_\n";
}
A common 'Perlism' is generating a list as something to loop over in this form:
for($str=~/./g) { print "the next character from \"$str\"=$_\n"; }
In this case the global match regex returns a list that is one character in turn from the string $str
, and assigns that value to $_
Instead of a regex, split
can be used in the same way or 'a'..'z'
, map
, etc.
I am investigating unpack
to generate a field by field interpretation of a string. I have always found unpack
to be less straightforward to the way my brain works, and I have never really dug that deeply into it.
As a simple case, I want to generate a list that is one character in each element from a string using unpack (yes -- I know I can do it with split(//,$str)
and /./g
but I really want to see if unpack can be used this way...)
Obviously, I can use a field list for unpack that is unpack("A1" x length($str), $str)
but is there some other way that kinda looks like globbing? ie, can I call unpack(some_format,$str)
either in list context or in a loop such that unpack will return the next group of character in the format group until $str is exausted?
I have read The Perl 5.12 Pack pod and the Perl 5.12 pack tutorial and the Perkmonks tutorial
Here is the sample code:
#!/usr/bin/perl
use warnings;
use strict;
my $str=join('',('a'..'z', 'A'..'Z')); #the alphabet...
$str=~s/(.{1,3})/$1 /g; #...in groups of three
print "str=$str\n\n";
for ($str=~/./g) {
print "regex: = $_\n";
}
for(split(//,$str)) {
print "split: \$_=$_\n";
}
for(unpack("A1" x length($str), $str)) {
print "unpack: \$_=$_\n";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pack
和unpack
模板可以使用括号对内容进行分组,就像正则表达式一样。该组之后可以进行重复计数。*
作为重复计数意味着“重复直到用完要打包/解包的东西”。您必须运行基准测试才能找出其中哪一个最快。
pack
andunpack
templates can use parentheses to group things much like regexps can. The group can be followed by a repeat count.*
as a repeat count means "repeat until you run out of things to pack/unpack".You'd have to run a benchmark to find out which of these is the fastest.