From: Rob Biedenharn Date: 2010-07-19T05:04:36+09:00 Subject: Re: Search Array On Jul 18, 2010, at 1:13 AM, Chad Weier wrote: > I have an array which is created by reading a csv file > > time = CSV.parse(File.read('time.csv')) > > time.csv contains > > valuea,a,100 > valueb,b,200 > valuec,c,300 > > I want to be able to find a particular value but I used > time.index 'valuea' and nothing displays. > > I am pretty sure it is because its an array of arrays > eg if i go puts time[0][0] it will display valuea > > The 2 dimensional array is what I need but I am not sure how to search > it. > Please help as I am very new at ruby. There's a particular method that applies in cases like this. You have an array of arrays and you want to find the first array having a given object as *its* first value. This is called an "association list" and you can get the array you want with: irb> time = CSV.parse(File.read('time.csv')) => [["valuea", "a", "100"], ["valueb", "b", "200"], ["valuec", "c", "300"]] irb> time.assoc('valuea') => ["valuea", "a", "100"] -Rob Rob Biedenharn Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab@GaslightSoftware.com http://GaslightSoftware.com/