[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Questions about string.pack() and string.unpack()
- From: Coda Highland <chighland@...>
- Date: Wed, 19 Nov 2014 18:46:43 -0800
On Wed, Nov 19, 2014 at 5:06 PM, Sean Conner <sean@conman.org> wrote:
>
> I'm looking over string.pack() and string.unpack() in Lua 5.3, and I have
> a few questions:
>
> 1. Why is string.unpack(fmt,s[,pos]) instead of string.unpack(s,fmt[,pos])?
> This means I can't do:
>
> magic,timestamp,tag = raw:unpack("I4I4I4")
>
> Instead, it's:
>
> magic,timestamp,tag = string.unpack("I4I4I4",raw)
>
> This also means I may have to cache the string table in a local variable
> in a module when it might be avoided. The former also "reads" better
> to me.
The rationale is because most functions (e.g. printf) take their
format strings as the first parameter.
Why not ("l4l4l4"):unpack(raw)? That avoids the caching the string table.
/s/ Adam