There's probably a simpler answer to this than the ways I've come up
with.
What's the best way to select columns from a two-dimensional array?

I build arrays to match excel-style formatting, like this but larger:
__________________

a = [
  [ 'A1', 'A2', 'A3' ],
  [ 'B1', 'B2', 'B3' ],
  [ 'C1', 'C2', 'C3' ]
]

def get_cols (multi_array, headers )

  indices = []
  headers.each { |val| indices << multi_array[0].index(val) }
  indices.compact!

  multi_array.map do |ar|
    indices.map { |idx| ar[idx] }
  end

end

get_cols a, %w(A1 A3)

=> [["A1", "A3"], ["B1", "B3"], ["C1", "C3"]]
__________________

I haven't been able to work out a way to do this without writing
long-winded code. Is there a simple solution?

Thanks.

-- 
Posted via http://www.ruby-forum.com/.