# Hash

**URL:** <https://www.ruby-forum.com/t/hash/185273>\
**Category:** Ruby\
**Created:** [March 17, 2010, 10:22am UTC](https://www.ruby-forum.com/t/hash/185273 "2010-03-17T10:22:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [March 17, 2010, 10:22am UTC](https://www.ruby-forum.com/t/hash/185273/1 "2010-03-17T10:22:27Z")

</div>

I have a hash, that stores values like this

grid[[0,1] =\> “Some value here”, [7,8] =\> "Some other value here, [3,4]  
=\> Some text here]]

This gets looped through and displayed as a table.

My problem is that the values are not fixed. The internal hash structure  
is variable though.

I would like to the the max value for

grid[[rows, column] =\> “value”]

the rows and columns.

I’ve looked at the base classes. Am I missing a function for doing this  
without iteration?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [March 17, 2010, 10:38am UTC](https://www.ruby-forum.com/t/hash/185273/2 "2010-03-17T10:38:30Z")

</div>

On Wed, Mar 17, 2010 at 10:22 AM, Sem P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I would like to the the max value for
> 
> grid[[rows, column] =\> “value”]
> 
> the rows and columns.
> 
> I’ve looked at the base classes. Am I missing a function for doing this  
> without iteration?

If I understand you correctly, you have a hash whose keys are 2  
element arrays. And you want to know which is the biggest row (first  
element) in all the keys and which one is the biggest column (second  
element), right?

If so, you can do this (the max function iterates using Hash#each  
internally, though):

irb(main):001:0\> h = {[0,1] =\> “1 value”, [7,8] =\> “another”, [3,9] =\>  
“the last one”}  
=\> {[7, 8]=\>“another”, [0, 1]=\>“1 value”, [3, 9]=\>“the last one”}  
irb(main):002:0\> h.max {|a,b| a[0] \<=\> b[0]}  
=\> [[7, 8], “another”]  
irb(main):003:0\> h.max {|a,b| a[0] \<=\> b[0]}[0][0]  
=\> 7  
irb(main):004:0\> h.max {|a,b| a[1] \<=\> b[1]}  
=\> [[3, 9], “the last one”]  
irb(main):005:0\> h.max {|a,b| a[1] \<=\> b[1]}[0][1]  
=\> 9

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [March 17, 2010, 10:44am UTC](https://www.ruby-forum.com/t/hash/185273/3 "2010-03-17T10:44:43Z")

</div>

Thanks Jesus

That is a perfect one liner solution.

JesÃºs Gabriel y GalÃ¡n wrote:

> On Wed, Mar 17, 2010 at 10:22 AM, Sem P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I would like to the the max value for
> > 
> > grid[[rows, column] =\> “value”]
> > 
> > the rows and columns.
> > 
> > I’ve looked at the base classes. Am I missing a function for doing this  
> > without iteration?
> 
> If I understand you correctly, you have a hash whose keys are 2  
> element arrays. And you want to know which is the biggest row (first  
> element) in all the keys and which one is the biggest column (second  
> element), right?
> 
> If so, you can do this (the max function iterates using Hash#each  
> internally, though):
> 
> irb(main):001:0\> h = {[0,1] =\> “1 value”, [7,8] =\> “another”, [3,9] =\>  
> “the last one”}  
> =\> {[7, 8]=\>“another”, [0, 1]=\>“1 value”, [3, 9]=\>“the last one”}  
> irb(main):002:0\> h.max {|a,b| a[0] \<=\> b[0]}  
> =\> [[7, 8], “another”]  
> irb(main):003:0\> h.max {|a,b| a[0] \<=\> b[0]}[0][0]  
> =\> 7  
> irb(main):004:0\> h.max {|a,b| a[1] \<=\> b[1]}  
> =\> [[3, 9], “the last one”]  
> irb(main):005:0\> h.max {|a,b| a[1] \<=\> b[1]}[0][1]  
> =\> 9
> 
> Hope this helps,
> 
> Jesus.

---

<div class="post-metadata">

**Author:** ![sempti](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sempti](https://www.ruby-forum.com/u/sempti)\
**Post date:** [March 17, 2010, 10:50am UTC](https://www.ruby-forum.com/t/hash/185273/4 "2010-03-17T10:50:31Z")

</div>

Very elegant.
