# Build struct from arrays and names

**URL:** <https://discourse.julialang.org/t/build-struct-from-arrays-and-names/66350>\
**Category:** General Usage\
**Tags:** struct, io, streaming\
**Created:** [August 13, 2021, 4:39pm UTC](https://discourse.julialang.org/t/build-struct-from-arrays-and-names/66350 "2021-08-13T16:39:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [August 13, 2021, 4:39pm UTC](https://discourse.julialang.org/t/build-struct-from-arrays-and-names/66350/1 "2021-08-13T16:39:55Z")

</div>

From a stream, I receive a number of arrays and strings as their field names.  
How can I dynamically build a structure from it?

Of course, trying to define a struct within the code generates

> ERROR: LoadError: syntax: “struct” expression not at top level

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 13, 2021, 4:57pm UTC](https://discourse.julialang.org/t/build-struct-from-arrays-and-names/66350/2 "2021-08-13T16:57:41Z")

</div>

You can’t declare structs on the fly like that. They are a part of the global state. But you can create nested `Dict`s or `NamedTuple`s.

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [August 13, 2021, 5:07pm UTC](https://discourse.julialang.org/t/build-struct-from-arrays-and-names/66350/3 "2021-08-13T17:07:22Z")

</div>

```julia
julia> ntup2 = (a = 1, b = 2)
(a = 1, b = 2)

julia> ntup2.b
2

```

Excellent, thx!

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [August 16, 2021, 10:35am UTC](https://discourse.julialang.org/t/build-struct-from-arrays-and-names/66350/4 "2021-08-16T10:35:59Z")

</div>

Follow-up thread: [Reshape into named tuple - General Usage - JuliaLang](https://discourse.julialang.org/t/reshape-into-named-tuple/66474)
