# Convert an array to a Tuple

**URL:** <https://forum.crystal-lang.org/t/convert-an-array-to-a-tuple/5735>\
**Category:** Help & Support\
**Created:** [June 3, 2023, 9:06pm UTC](https://forum.crystal-lang.org/t/convert-an-array-to-a-tuple/5735 "2023-06-03T21:06:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 3, 2023, 9:06pm UTC](https://forum.crystal-lang.org/t/convert-an-array-to-a-tuple/5735/1 "2023-06-03T21:06:36Z")

</div>

Hi, today I read the documentation about Tuple. I saw there is a function **.from(array : Array)**. But I can’t use it to have the result I expected.

Somebody can explain me how to convert an array to a Tuple ?  
I mean convert, or get a Tuple with the array content

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [June 3, 2023, 9:52pm UTC](https://forum.crystal-lang.org/t/convert-an-array-to-a-tuple/5735/2 "2023-06-03T21:52:47Z")

</div>

Can you share the error/unexpected result you’re getting? The catch with this method is that you have to know the amount items and their types. E.g. you can just do `Tuple.from([1, 2, 3])` you’d have to do `Tuple(Int32, Int32, Int32).from([1, 2, 3])`, which would error if there are more or less than 3 arguments, or the types do not match those in the type vars.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 3, 2023, 10:15pm UTC](https://forum.crystal-lang.org/t/convert-an-array-to-a-tuple/5735/3 "2023-06-03T22:15:11Z")

</div>

This is my main problem, I need to pass the type of each element.

Just to explain you, I need to make a Tuple from an Array of ISM::SoftwareInformation, but I don’t know the size of this array at the beginning, it’s only after my software check all recorded SoftwareInformation on the database.

How can I pass the type multiplicated by the number of elements in my array ?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [June 3, 2023, 10:49pm UTC](https://forum.crystal-lang.org/t/convert-an-array-to-a-tuple/5735/4 "2023-06-03T22:49:21Z")

</div>

I don’t think you can if the amount of items is only known at runtime. Tuples are a compile time construct, you can’t just dynamically create them. Might be best to take a step back and try a different approach.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 3, 2023, 10:51pm UTC](https://forum.crystal-lang.org/t/convert-an-array-to-a-tuple/5735/5 "2023-06-03T22:51:08Z")

</div>

Oh okay I wasn’t sure. I just try to found way to improve speed time.

It can be a good idea for example to generate a json file of this array, and after load it as a Tuple ?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [June 3, 2023, 10:54pm UTC](https://forum.crystal-lang.org/t/convert-an-array-to-a-tuple/5735/6 "2023-06-03T22:54:08Z")

</div>

If the data is truly dynamic, then an array is probably the correct data type.

---

<div class="post-metadata">

**Author:** ![Fulgurance](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/fulgurance/32/1463_2.png) [@Fulgurance](https://forum.crystal-lang.org/u/Fulgurance)\
**Post date:** [June 5, 2023, 9:05am UTC](https://forum.crystal-lang.org/t/convert-an-array-to-a-tuple/5735/7 "2023-06-05T09:05:48Z")

</div>

Yeah make sense now. Thanks !
