Chinaunix首页 | 论坛 | 博客
  • 博客访问: 34921
  • 博文数量: 4
  • 博客积分: 185
  • 博客等级: 民兵
  • 技术积分: 45
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-19 10:25
文章分类

全部博文(4)

文章存档

2012年(4)

最近访客

分类: Python/Ruby

2012-04-01 16:27:28

The unpack function gives only read access, but is faster when you have many substrings to extract.

# get a 5-byte string, skip 3 bytes,
# then grab two 8-byte strings, then the rest;
 # (NB: only works on ASCII data, not Unicode)
($leading, $s1, $s2, $trailing) = unpack("A5 x3 A8 A8 A*", $data);
 # split at 5-byte boundaries
@fivers = unpack("A5" x (length($string)/5), $string);
# chop string into individual single-byte characters
@chars = unpack("A1" x length($string), $string);
  1. $string = "good good start, day day up!";
  2. ($word1, $word2,$word3)=unpack("A5 x5 A5 A*",$string);
  3. print $word1."+".$word2."+".$word3;
  4. print "\n";
  5. @fivers = unpack("A5" x (length($string)/5), $string);
  6. print "@fivers";
  7. print "\n";
  8. @chars = unpack("A1" x (length($string)),$string);
  9. print "@chars";
  10. print "\n";
  11. #result:
  12. good+start+, day day up!
  13. good good start , day day
  14. g o o d g o o d s t a r t , d a y d a y u p !

 
阅读(2837) | 评论(0) | 转发(0) |
0

上一篇:substr()选读和修改子串

下一篇:没有了

给主人留下些什么吧!~~