From: gwtmp01@... Date: 2006-09-14T00:54:29+09:00 Subject: Re: arbitrary indexes On Sep 12, 2006, at 12:18 PM, Austin Ziegler wrote: > On 9/12/06, Jason Nordwick wrote: >> There has to be an easier way to do this. >> >> If I have v=(1..99).to_a and I want to index the 20th to 30th >> elements, this is simply v[20,10]. But if I have a list of indexes >> i=[9, 11, 35, 47, 48, 55, 58, 63, 92, 96] that I want, I can't >> seem to find a simpler way than i.map{|x|v[x]} which seem like an >> aweful lot of syntax just for what is essentially "v[i]". Is there >> an easier way to do this? > >>> v.values_at(9, 11, 35, 47, 48, 55, 58, 63, 92, 96) > => [10, 12, 36, 48, 49, 56, 59, 64, 93, 97] Another nice thing about values_at is that it understands ranges: a = Array.new(10) { |x| x*2 } #=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] b = a.values_at(1,3,5..7) #=> [2, 6, 10, 12, 14] Gary Wright