J
J. Romano
Tassilo said:here's one that I find useful, namely the '/' construct.
The template preceeding the slash is used as a count
argument for the template following the slash:
Note how this can be combined with @:
my @x = unpack '@2c/C', "\x03\x00\x01\xff\x03";
print "@x\n",
__END__
255
Before that thread, I wasn't aware that I could use '@' like that
to start the position of unpacking. Before I learned about '@', I
would always use substr() to remove the beginning part of the string
that I wanted to ignore.
But that brings me to another question, one that concerns the usage
of the '/' construct. I've used that construct before, but I could
never find an easy way to figure out my offset into the string after
I've used it.
Let me clarify with an example. Say I have the following piece of
code:
my $string = "\x00\x01\x03\x00\x01\xff\x01\x0f";
# or I could say: my $sring = v0.1.3.0.1.255.1.15;
my @x = unpack '@2 c/C', $string;
That would make @x have three elements: 0, 1, and 255.
Now let's say that, after examining the contents of @x, I decide to
read in data for @y, starting where @x left off:
my @y = unpack '@6 c/C', $string;
That would make @y have only one element: 15.
However, in order to know that the second unpack command must start
at offset 6, I would have to calculate how many elements are in @x,
multiply that number by the space each element in @x takes (that is,
took up in the packed string), and add it to 2 (the first unpack()
offset) and add 1 (for the 'c' construct). (This may not be too
complicated to do now, but it would be much more difficult to do with
a more complex unpack string, like "w/(c4 i L Z20)".)
Is there a way for an unpack string to return the offset so that a
second unpack string can use it with the '@' construct? This would
make dealing with variable-length extractions (for example, with 'w/'
and 'Z*') much easier. I have read the "perldoc -f pack" and "perldoc
perlpacktut" pages but couldn't find anything about it (but maybe I
missed something).
Thanks in advance for any help.
-- Jean-Luc