2.1.3. Arrays / Lists#
When working with binary formats, it’s common to store multiple objects of the same type in a sequence, such as a list or array. Caterpillar simplifies this task by allowing easy access to items and defining arrays with both static and dynamic sizes.
For example, let’s consider the PLTE chunk from the PNG format, which stores a sequence of three-byte RGB objects. To define an array of RGB objects, you can use the following syntax:
>>> PLTEChunk = RGB[this.length / 3]
>>> PLTEChunk = RGB.__struct__[ContextPath("obj.length") / 3]
Added in version 2.2.0: The syntax will be changed once __class_getitem__ is implemented by
any c.Struct
instance.
Important
The length of the array is determined dynamically. In this case, the length is
calculated by dividing the chunk’s length
field by 3, since each RGB
object occupies 3 bytes.