Search Array

C

Chad Weier

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.
 
W

w_a_x_man

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.

time.find{|x| x[0]=="valuea"}
 
J

Jean-Julien Fleck

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 =3D biarray.index {|a| a.include?(obj)}
index2 =3D biarray[index1].index(obj)
return index1,index2
end

and to check the behavior:

time =3D [["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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 
R

Rob Biedenharn

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
(e-mail address removed) http://AgileConsultingLLC.com/
(e-mail address removed) http://GaslightSoftware.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top