# Two deminsional array assignment issue

**URL:** <https://forum.crystal-lang.org/t/two-deminsional-array-assignment-issue/4447>\
**Category:** Help & Support\
**Created:** [March 6, 2022, 1:04pm UTC](https://forum.crystal-lang.org/t/two-deminsional-array-assignment-issue/4447 "2022-03-06T13:04:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kefahi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kefahi/32/598_2.png) [@kefahi](https://forum.crystal-lang.org/u/kefahi)\
**Post date:** [March 6, 2022, 1:04pm UTC](https://forum.crystal-lang.org/t/two-deminsional-array-assignment-issue/4447/1 "2022-03-06T13:04:14Z")

</div>

Hello,  
I’m puzzled with the outcome of this simple code.  
The intention is to assign a unique number in each “cell”:

```auto
heirs = Array.new(2, Array.new(2, 0_i32))
(0..3).each { |i|
  heirs[i//2][i%2] = i+1
}

p! heirs

```

This is the result I get (based on Crystal 1.3.2)

```auto
heirs # => [[3, 4], [3, 4]]

```

The expected outcome should have been:

```auto
heirs # => [[1, 2], [3, 4]]

```

Any clue on my wrong doing?

Thanks!

---

<div class="post-metadata">

**Author:** ![kefahi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kefahi/32/598_2.png) [@kefahi](https://forum.crystal-lang.org/u/kefahi)\
**Post date:** [March 6, 2022, 1:11pm UTC](https://forum.crystal-lang.org/t/two-deminsional-array-assignment-issue/4447/2 "2022-03-06T13:11:47Z")

</div>

Update: My guess is that only one sub-array is created instead of two.  
But, if so, how do I instruct crystal to create two sub-arrays?

---

<div class="post-metadata">

**Author:** ![hholst](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hholst/32/1573_2.png) [@hholst](https://forum.crystal-lang.org/u/hholst)\
**Post date:** [March 6, 2022, 1:14pm UTC](https://forum.crystal-lang.org/t/two-deminsional-array-assignment-issue/4447/3 "2022-03-06T13:14:23Z")

</div>

Why can’t this library support dense array notation like numpy? The representation of matrices as a list of lists is beyond inefficient it should be illegal.

Have a look at this instead if you want to work with real matrices

> **[GitHub - mverzilli/crystalla: Crystal library for Numerical Methods. It binds...](https://github.com/mverzilli/crystalla)**
>
> Crystal library for Numerical Methods. It binds to LAPACK and is unashamedly inspired by Numpy. - GitHub - mverzilli/crystalla: Crystal library for Numerical Methods. It binds to LAPACK and is una...

PS. This works right? It creates an Array of two (distinct) arrays. You’re only calling the .new method once in the inner section so there is just one inner Array - just as you already described.

```crystal
heir = [[0,0],[0,0]]
(0..3).each { |i|
  heirs[i//2][i%2] = i+1
}

```

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [March 6, 2022, 1:26pm UTC](https://forum.crystal-lang.org/t/two-deminsional-array-assignment-issue/4447/4 "2022-03-06T13:26:57Z")

</div>

> Update: My guess is that only one sub-array is created instead of two.

That’s exactly the reason. When you pass a value to `Array.new(size, value)`, that value is used to initialize every position.

You can use the block version to use different values in every position. Try this:

```crystal
heirs = Array.new(2) { Array.new(2, 0_i32) }
(0..3).each { |i|
  heirs[i//2][i % 2] = i + 1
}

p! heirs

```

You can also use the block version to directly initialize the array with the values you need:

```crystal
heirs = Array.new(2) do |i|
  Array.new(2) do |j|
    2*i + j + 1
  end
end
p! heirs

```

---

<div class="post-metadata">

**Author:** ![kefahi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kefahi/32/598_2.png) [@kefahi](https://forum.crystal-lang.org/u/kefahi)\
**Post date:** [March 6, 2022, 1:42pm UTC](https://forum.crystal-lang.org/t/two-deminsional-array-assignment-issue/4447/5 "2022-03-06T13:42:40Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![kefahi](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kefahi/32/598_2.png) [@kefahi](https://forum.crystal-lang.org/u/kefahi)\
**Post date:** [March 6, 2022, 1:44pm UTC](https://forum.crystal-lang.org/t/two-deminsional-array-assignment-issue/4447/6 "2022-03-06T13:44:55Z")

</div>

Awesome!  
The block version is concise and makes sense.  
Thank you.
