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);
- $string = "good good start, day day up!";
- ($word1, $word2,$word3)=unpack("A5 x5 A5 A*",$string);
- print $word1."+".$word2."+".$word3;
- print "\n";
- @fivers = unpack("A5" x (length($string)/5), $string);
- print "@fivers";
- print "\n";
- @chars = unpack("A1" x (length($string)),$string);
- print "@chars";
- print "\n";
- #result:
- good+start+, day day up!
- good good start , day day
- g o o d g o o d s t a r t , d a y d a y u p !
阅读(2837) | 评论(0) | 转发(0) |