in reply to unpack 'C/a*' and context weirdness
So to get both pieces of information I have to call unpack twice?No... you might have missed that spot in the docs that say "back up a byte". I will use it to effectively unpack the same byte twice, with one unpack(), and one template.my $len = unpack 'C/a*', $packed; my ($data) = unpack 'C/a*', $packed;
resulting inprint join ":", unpack 'CXC/a*', $packed;
Another option, though less user-friendly, would be to use the '@' template, which you can use to reposition the current pointer — think of it as a form of seek().19:the quick brown fox
the "0" being the absolute position — here, the start of the string. Actually, it turns out that if you omit it, zero is used as a default.print join ":", unpack 'C@0C/a*', $packed;
The user-unfriendlyness is in the fact that you need to know the exact absolute position to seek to — making it impractical to encorporate it in the middle of a larger template — and not a relative one. Well, that's what the "X" is for, if you want to go backwards, and "x", to go forward.
which results in:print join ":", unpack 'C@C/a*X15a5x7a*', $packed;
19:the quick brown fox:quick:fox
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: unpack 'C/a*' and context weirdness
by BrowserUk (Patriarch) on Feb 15, 2003 at 02:07 UTC |