Itterate over an Array Extracting Data

D

Daniel Russia

So I have the following array

people = ["Employee 1", "Secretary", "Male", "54"]
["Employee 2", "Manager", "Female", "44"]
["Employee 3", "Chef", "Female", "22"]

I would like to iterate over this array pulling out just the ages. How
can I do this?

I tried the following:

x = 0
while x < people.length
puts people[x][3]
x += 1
end

How could I put the results into an array? IRB doesn't like my code.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

So I have the following array

people = ["Employee 1", "Secretary", "Male", "54"]
["Employee 2", "Manager", "Female", "44"]
["Employee 3", "Chef", "Female", "22"]

I would like to iterate over this array pulling out just the ages. How
can I do this?

I tried the following:

x = 0
while x < people.length
puts people[x][3]
x += 1
end

How could I put the results into an array? IRB doesn't like my code.
["Employee 2", "Manager", "Female", "44"]
["Employee 3", "Chef", "Female", "22"]

people # => ["Employee 1", "Secretary", "Male", "54"]

#you (presumably) wanted an array of arrays, it would need to look like the
one below
people = [["Employee 1" , "Secretary" , "Male" , "54"],
["Employee 2" , "Manager" , "Female" , "44"],
["Employee 3" , "Chef" , "Female" , "22"]]

ages = people.map{|p| p[3] }

ages # => ["54", "44", "22"]

people # => [["Employee 1", "Secretary", "Male", "54"], ["Employee 2",
"Manager", "Female", "44"], ["Employee 3", "Chef", "Female", "22"]]
 
R

Rimantas Liubertas

So I have the following array
people =3D =C2=A0["Employee 1", "Secretary", "Male", "54"]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0["Employee 2", "Manager", "Female", "44= "]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0["Employee 3", "Chef", "Female", "22"]

I would like to iterate over this array pulling out just the ages. How
can I do this?

I tried the following:

x =3D 0
while x < people.length
puts people[x][3]
x +=3D 1
end

How could I put the results into an array? IRB doesn't like my code.
people =3D [["Employee 1", "Secretary", "Male", "54"],["Employee 2", "M=
anager", "Female", "44"],["Employee 3", "Chef", "Female", "22"]]
=3D> [["Employee 1", "Secretary", "Male", "54"], ["Employee 2",
"Manager", "Female", "44"], ["Employee 3", "Chef", "Female", "22"]]
ages =3D people.collect{|person| person[3]}
=3D> ["54", "44", "22"]


Regards,
Rimantas
 
P

Paul Smith

people.each {|person| puts person[3]}

PS I had to enter your array differently - what you typed here didn't work.
people =3D [["Employee 1", "Secretary", "Male", "54"],
["Employee 2", "Manager", "Female", "44"],
["Employee 3", "Chef", "Female", "22"]]


So I have the following array

people =3D =A0["Employee 1", "Secretary", "Male", "54"]
=A0 =A0 =A0 =A0 =A0["Employee 2", "Manager", "Female", "44"]
=A0 =A0 =A0 =A0 =A0["Employee 3", "Chef", "Female", "22"]

I would like to iterate over this array pulling out just the ages. How
can I do this?

I tried the following:

x =3D 0
while x < people.length
puts people[x][3]
x +=3D 1
end

How could I put the results into an array? IRB doesn't like my code.



--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

So I have the following array

people = ["Employee 1", "Secretary", "Male", "54"]
["Employee 2", "Manager", "Female", "44"]
["Employee 3", "Chef", "Female", "22"]

I would like to iterate over this array pulling out just the ages. How
can I do this?

I tried the following:

x = 0
while x < people.length
puts people[x][3]
x += 1
end

How could I put the results into an array? IRB doesn't like my code.
SciTe doesn't use monospaced font by default.
 
H

Harry Kakueki

So I have the following array

people = ["Employee 1", "Secretary", "Male", "54"]
["Employee 2", "Manager", "Female", "44"]
["Employee 3", "Chef", "Female", "22"]

I would like to iterate over this array pulling out just the ages. How
can I do this?

I tried the following:

x = 0
while x < people.length
puts people[x][3]
x += 1
end

How could I put the results into an array? IRB doesn't like my code.

people = [["Employee 1", "Secretary", "Male", "54"],
["Employee 2", "Manager", "Female", "44"],
["Employee 3", "Chef", "Female", "22"]]


p people.transpose[3]


Harry
 
T

Tommy Nordgren

So I have the following array

people = ["Employee 1", "Secretary", "Male", "54"]
["Employee 2", "Manager", "Female", "44"]
["Employee 3", "Chef", "Female", "22"]
Here you have THREE arrays, of which to are not assigned ANYWHERE.
You forgot the outer brackets and the commans between elements of the
outer array.
I would like to iterate over this array pulling out just the ages. How
can I do this?

I tried the following:

x = 0
while x < people.length
puts people[x][3]
x += 1
end

How could I put the results into an array? IRB doesn't like my code.
 

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

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top