Using of array in PyTeal

Hi everybody,
I’m working with Smart Contract and so I’m using PyTeal.
I need to declare and fill an array. Is it possible? How can I do both in PyTeal?
Thank you!

TEAL does not supports array.
The only two types of values are: byte slices and uint.
You can however simulate arrays using byte slices.

Furthermore, the storage is key/value. Using keys 0,1,… you can also simulate an array.

But in any case, your array will need to be constant-size (i.e., independent of the number of users that use the application).

If you need to store an amount of data linear in the number of users, you need to use local storage (and have the users opt in).

1 Like

Yes, I need a constant-size array.
However, I didn’t understand how I can use byte slices to simulate array.

It depends on the type of data you are storing in the array.
But let’s say you store 4-byte values.
Then the first value can be stored at indices 0-3, the second value at indices 4-7, …
TEAL has operations to extract substrings easily: Byte Operators — PyTeal documentation

If you need an array of uint, you can first convert uint into 8-byte strings usinghttps://pyteal.readthedocs.io/en/latest/data_type.html#conversion

1 Like