From: Josh Cheek Date: 2009-09-17T18:46:36+09:00 Subject: Re: Itterate over an Array Extracting Data --000e0cd488301704e30473c2e454 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Sep 17, 2009 at 4:34 AM, Daniel Russia wrote: > 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. > -- > Posted via http://www.ruby-forum.com/. > > 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"]] --000e0cd488301704e30473c2e454--