# Search Array

**URL:** <https://www.ruby-forum.com/t/search-array/191764>\
**Category:** Ruby\
**Created:** [July 18, 2010, 7:10am UTC](https://www.ruby-forum.com/t/search-array/191764 "2010-07-18T07:10:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 18, 2010, 7:10am UTC](https://www.ruby-forum.com/t/search-array/191764/1 "2010-07-18T07:10:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 18, 2010, 9:04am UTC](https://www.ruby-forum.com/t/search-array/191764/2 "2010-07-18T09:04:19Z")

</div>

On Jul 18, 12:13 am, Chad W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.
> 
> Posted viahttp://www.ruby-forum.com/.

time.find{|x| x[0]==“valuea”}

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 18, 2010, 9:30am UTC](https://www.ruby-forum.com/t/search-array/191764/3 "2010-07-18T09:30:04Z")

</div>

Hello Chad,

> The 2 dimensional array is what I need but I am not sure how to search  
> it.

If you want to get the index couple, you could do something like this:

def index\_biarray(biarray,obj)  
index1 = biarray.index {|a| a.include?(obj)}  
index2 = biarray[index1].index(obj)  
return index1,index2  
end

and to check the behavior:

time = [[“valuea”,“a”,100],[“valueb”,“b”,200],[“valuec”,“c”,300]]

puts index\_biarray(time,“valuea”).join(’,’)  
puts index\_biarray(time,200).join(’,’)  
puts index\_biarray(time,‘c’).join(’,’)

Cheers,

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 18, 2010, 10:05pm UTC](https://www.ruby-forum.com/t/search-array/191764/4 "2010-07-18T22:05:11Z")

</div>

On Jul 18, 2010, at 1:13 AM, Chad W. wrote:

> 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 B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![superwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@superwick](https://www.ruby-forum.com/u/superwick)\
**Post date:** [July 19, 2010, 2:52am UTC](https://www.ruby-forum.com/t/search-array/191764/5 "2010-07-19T02:52:50Z")

</div>

Thank you so much. Very helpful I’ve got the program working!
