in [Ruby]

Prev: [ANN] lorax 0.1.0 Released
Next: SOCKSSocket working?
From: Sem Ptiri on 17 Mar 2010 05:44 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 Ptiri <rubyforum (a)chthonic.co.za> > 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. -- Posted via http://www.ruby-forum.com/.
From: Sem Ptiri on 17 Mar 2010 05:50
Very elegant. -- Posted via http://www.ruby-forum.com/. |