有弟兄用过vec函数的吗?请指教一下

有弟兄用过vec函数的吗?请指教一下

有弟兄用过vec函数的吗?请指教一下
请问一下vec()这个函数到底用来做什么呢?
官方文档里的怎么也看不明白。
OFFSET的具体作用是什么呢?
能不能帮我举个例子,先谢过
函数名 vec 函数名 vec
调用语法 retval = vec (expr, offset, bits);
解说 顾名思义,vec即矢量(vector)函数,它把简单变量vector的值看作多块(维)数据,每块含一定数目的位,合起来即一个矢量数据。每次的调用访问其中一块数据,可以读取,也可以写入。参数offset就象数组下标一样,提出访问哪一块,0为第一块,依次类推,要注意的是访问次序是从右到左的,即第一块在最右边。参数bits指定每块中的位数,可以为1,2,4,8,16或32。
例子 1 : #!/usr/local/bin/perl
2 :
3 : $vector = pack ("B*", "11010011");
4 : $val1 = vec ($vector, 0, 4);
5 : $val2 = vec ($vector, 1, 4);
6 : print ("high-to-low order values: $val1 and $val2\n");
7 : $vector = pack ("b*", "11010011");
8 : $val1 = vec ($vector, 0, 4);
9 : $val2 = vec ($vector, 1, 4);
10: print ("low-to-high order values: $val1 and $val2\n");
结果 high-to-low order values: 3 and 13
low-to-high order values: 11 and 12
------------------------
摘自flamephoenix的perl教程
谢谢,结合文档...
谢谢,结合文档上那个例子就搞得明白了


[color=#FF6600] my $foo = '';
vec($foo, 0, 32) = 0x5065726C; # 'Perl'

# $foo eq "Perl" eq "\x50\x65\x72\x6C", 32 bits
print vec($foo, 0, 8); # prints 80 == 0x50 == ord('P')

vec($foo, 2, 16) = 0x5065; # 'PerlPe'
vec($foo, 3, 16) = 0x726C; # 'PerlPerl'
vec($foo, 8, 8) = 0x50; # 'PerlPerlP'
vec($foo, 9, 8) = 0x65; # 'PerlPerlPe'
vec($foo, 20, 4) = 2; # 'PerlPerlPe' . "\x02"
vec($foo, 21, 4) = 7; # 'PerlPerlPer'
# 'r' is "\x72"
vec($foo, 45, 2) = 3; # 'PerlPerlPer' . "\x0c"
vec($foo, 93, 1) = 1; # 'PerlPerlPer' . "\x2c"
vec($foo, 94, 1) = 1; # 'PerlPerlPerl'
# 'l' is "\x6c"

[/color]

还有重要的这个:

[color=#008000] If BITS is 8, ``elements'' coincide with bytes of the input string.

If BITS is 16 or more, bytes of the input string are grouped into chunks of size BITS/8, and each group is converted to a number as with pack()/unpack() with big-endian formats n/N (and analogously for BITS==64). See pack for details.

If bits is 4 or less, the string is broken into bytes, then the bits of each byte are broken into 8/BITS groups. Bits of a byte are numbered in a little-endian-ish way, as in 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80. For example, breaking the single input byte chr(0x36) into two groups gives a list (0x6, 0x3); breaking it into 4 groups gives (0x2, 0x1, 0x3, 0x0).
[/color]