# Itterate over an Array Extracting Data

**URL:** <https://www.ruby-forum.com/t/itterate-over-an-array-extracting-data/175667>\
**Category:** Ruby\
**Created:** [September 17, 2009, 11:34am UTC](https://www.ruby-forum.com/t/itterate-over-an-array-extracting-data/175667 "2009-09-17T11:34:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@danielr](https://www.ruby-forum.com/u/danielr)\
**Post date:** [September 17, 2009, 11:34am UTC](https://www.ruby-forum.com/t/itterate-over-an-array-extracting-data/175667/1 "2009-09-17T11:34:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danielr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@danielr](https://www.ruby-forum.com/u/danielr)\
**Post date:** [September 17, 2009, 11:47am UTC](https://www.ruby-forum.com/t/itterate-over-an-array-extracting-data/175667/2 "2009-09-17T11:47:11Z")

</div>

On Thu, Sep 17, 2009 at 4:34 AM, Daniel R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> people = [“Employee 1”, “Secretary”, “Male”, “54”]  
> [“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”]]

---

<div class="post-metadata">

**Author:** ![danielr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@danielr](https://www.ruby-forum.com/u/danielr)\
**Post date:** [September 17, 2009, 11:47am UTC](https://www.ruby-forum.com/t/itterate-over-an-array-extracting-data/175667/3 "2009-09-17T11:47:47Z")

</div>

> 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”]]  
> > =\> [[“Employee 1”, “Secretary”, “Male”, “54”], [“Employee 2”,  
> > “Manager”, “Female”, “44”], [“Employee 3”, “Chef”, “Female”, “22”]]
> 
> > ages = people.collect{|person| person[3]}  
> > =\> [“54”, “44”, “22”]

Regards,  
Rimantas

---

<div class="post-metadata">

**Author:** ![danielr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@danielr](https://www.ruby-forum.com/u/danielr)\
**Post date:** [September 17, 2009, 11:50am UTC](https://www.ruby-forum.com/t/itterate-over-an-array-extracting-data/175667/4 "2009-09-17T11:50:07Z")

</div>

On Thu, Sep 17, 2009 at 4:34 AM, Daniel R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Sorry for the improper spacing, I’m on my Windows computer, forgot that  
> SciTe doesn’t use monospaced font by default.

---

<div class="post-metadata">

**Author:** ![danielr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@danielr](https://www.ruby-forum.com/u/danielr)\
**Post date:** [September 17, 2009, 11:48am UTC](https://www.ruby-forum.com/t/itterate-over-an-array-extracting-data/175667/5 "2009-09-17T11:48:57Z")

</div>

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

PS I had to enter your array differently - what you typed here didn’t  
work.

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

On Thu, Sep 17, 2009 at 10:34 AM, Daniel R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

–  
Paul S.  
[http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![danielr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@danielr](https://www.ruby-forum.com/u/danielr)\
**Post date:** [September 17, 2009, 11:51am UTC](https://www.ruby-forum.com/t/itterate-over-an-array-extracting-data/175667/6 "2009-09-17T11:51:05Z")

</div>

Josh C. wrote:

> On Thu, Sep 17, 2009 at 4:34 AM, Daniel R.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > Sorry for the improper spacing, I’m on my Windows computer, forgot that  
> > SciTe doesn’t use monospaced font by default.

Thanks for everyone’s help.

---

<div class="post-metadata">

**Author:** ![danielr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@danielr](https://www.ruby-forum.com/u/danielr)\
**Post date:** [September 17, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/itterate-over-an-array-extracting-data/175667/7 "2009-09-17T15:01:32Z")

</div>

On 17 sep 2009, at 11.34, Daniel R. wrote:

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

> ## How could I put the results into an array? IRB doesn’t like my code.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

* * *

The three things an undertaker should never say to a client:  
-Nice doing business with you.  
-Welcome back.  
-Have a nice day. (The King of ID)

---

<div class="post-metadata">

**Author:** ![danielr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f17d59/32.png) [@danielr](https://www.ruby-forum.com/u/danielr)\
**Post date:** [September 17, 2009, 11:55am UTC](https://www.ruby-forum.com/t/itterate-over-an-array-extracting-data/175667/8 "2009-09-17T11:55:59Z")

</div>

On Thu, Sep 17, 2009 at 6:34 PM, Daniel R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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
