# Is it possible to create register of registers (e.g., 2D array)?

**URL:** <https://forum.p4.org/t/is-it-possible-to-create-register-of-registers-e-g-2d-array/916>\
**Category:** Getting Started with P4\
**Created:** [August 11, 2023, 6:10am UTC](https://forum.p4.org/t/is-it-possible-to-create-register-of-registers-e-g-2d-array/916 "2023-08-11T06:10:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chamara](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@chamara](https://forum.p4.org/u/chamara)\
**Post date:** [August 11, 2023, 6:10am UTC](https://forum.p4.org/t/is-it-possible-to-create-register-of-registers-e-g-2d-array/916/1 "2023-08-11T06:10:51Z")

</div>

Hi all,

I am using on V1 model and trying to store a selected feature value(e.g., packet length) for each packet or upto a certain number of packets (e.g., let’s say first 100 packets). In a normal programming language we can easily do this by creating 2D array where rows for flows and columns for each packet. But I am not sure whether this can be done in P4 using V1 model. Any suggestion will be highly appreciated.

Chamara

---

<div class="post-metadata">

**Author:** ![andyfingerhut](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/andyfingerhut/32/17_2.png) [@andyfingerhut](https://forum.p4.org/u/andyfingerhut)\
**Post date:** [August 11, 2023, 7:08am UTC](https://forum.p4.org/t/is-it-possible-to-create-register-of-registers-e-g-2d-array/916/2 "2023-08-11T07:08:16Z")

</div>

There is no direct way to declare and use a 2-dimensional array that I am aware of.

There is a fairly straightforward workaround. Suppose you wish you had a 2-dimensional array my\_reg[A][B]. Instead declare a 1-dimensional array my\_reg[A\*B], and whenever you wish to access element my\_reg[i][j], you instead access element my\_reg[i\*B+j]. If one of the array dimensions A or B is a power of 2, then multiplying by that value is equivalent to a left shift by the proper number of bit positions.

---

<div class="post-metadata">

**Author:** ![chamara](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@chamara](https://forum.p4.org/u/chamara)\
**Post date:** [August 11, 2023, 1:33pm UTC](https://forum.p4.org/t/is-it-possible-to-create-register-of-registers-e-g-2d-array/916/3 "2023-08-11T13:33:10Z")

</div>

Thank you very much.

This solved the problem.
