# Hash#each\_with\_index

**URL:** <https://forum.crystal-lang.org/t/hash-each-with-index/1576>\
**Category:** Crystal Contrib\
**Created:** [January 2, 2020, 2:57am UTC](https://forum.crystal-lang.org/t/hash-each-with-index/1576 "2020-01-02T02:57:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [January 2, 2020, 2:57am UTC](https://forum.crystal-lang.org/t/hash-each-with-index/1576/1 "2020-01-02T02:57:11Z")

</div>

I know if I have named tuples I can “dereference” them (or whatever it’s called) ex: `each_with_index`

```auto
a = {"abc": "def", "name": "value"}
a.each_with_index{|first, second, index|
  puts first, second, index # works!
}

```

It would be sometimes convenient if we could do the same thing with other things like `Hash#each_with_index` or what not, to get the key, value, and index in one fell swoop.  
Today it just yields:

```auto
$ cat test.cr
a = {3 => 4, 5 => 6}
a.each_with_index{|k, v, i|

}
$ crystal test.cr
Showing last frame. Use --error-trace for full trace.

In test.cr:2:18

 2 | a.each_with_index{|k, v, i|
                      ^
Error: too many block arguments (given 3, expected maximum 2)

```

Requiring splitting of the key/value afterward.

Anyway just a feature request for discussion.  
Cheers!

---

<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:** [January 2, 2020, 10:31am UTC](https://forum.crystal-lang.org/t/hash-each-with-index/1576/2 "2020-01-02T10:31:17Z")

</div>

a.each\_with\_index{|(k, v) , i|

Note the parentheses
