If you want the other, you would simply change the last line of the makeHash function from :

output

to...

require 'rubygems'
require 'nano/hash'
output.traverse { |key,value| [ key, value.length == 1 ? value[0] : value ] }

...
done. ... and yes, if it says can't find nano/hash ...

do:
gem install facets
...from the command line
...documentation is @ http://facets.rubyforge.org

j.

On 11/30/05, Jeff Wood <[EMAIL PROTECTED]> wrote:
assuming that you ALWAYS have only two arrays ...

def makeHash( pArray )
  output = {}

  pArray[1].each_with_index do |curr_key,curr_index|
    output[ curr_key ] ||= []
    output[ curr_key ] << pArray[0][ curr_index ]
  end

  output
end

puts makeHash( [ [ "a", "b", "c" ], [ 1, 2, 3 ] ] ).inspect
puts makeHash( [ [ "a", "b", "c" ], [ 1, 2, 2 ] ] ).inspect

... at least that's how I'd do it... and I'd always have the elements be arrays, I'd rather be consistent.

j.


On 11/30/05, Chris McMahon <[EMAIL PROTECTED] > wrote:
Hi all....

Again, apologies for the OT, stop me if it's not amusing...

Suppose I have an array of arrays like

[[A, B, C][1,2,3]]

I can easily make a hash (using each.with_index) where one value is
the lookup value for the other value:

1=>A
2=>B
3=>C

Now suppose I have a AoA like

[[A, B, C,][1,2,2]]

Is there a readable way to construct a hash like

1=>A
2=>[B, C]

?

Thanks,
-Chris

_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general



--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood



--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood
_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general

Reply via email to