# Split an element in an array into different fields using :

**URL:** <https://rubytalk.org/t/split-an-element-in-an-array-into-different-fields-using/43155>\
**Category:** ruby-talk\
**Created:** [19 December 2007 15:41 UTC](https://rubytalk.org/t/split-an-element-in-an-array-into-different-fields-using/43155 "2007-12-19T15:41:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Loftus](https://avatars.discourse-cdn.com/v4/letter/p/bc79bd/32.png) [@Peter\_Loftus](https://rubytalk.org/u/Peter_Loftus)\
**Post date:** [19 December 2007 15:41 UTC](https://rubytalk.org/t/split-an-element-in-an-array-into-different-fields-using/43155/1 "2007-12-19T15:41:38Z")

</div>

Hey guys so i have a example of splitting elements in an array and  
getting the third word. Im using spaces to split it. Anyone know how to  
split it if i need to use : instead of spaces

arr = File.readlines("sample.txt")  
arr.map! {|line| line.split("\s")[2]}

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [19 December 2007 15:48 UTC](https://rubytalk.org/t/split-an-element-in-an-array-into-different-fields-using/43155/2 "2007-12-19T15:48:12Z")

</div>

line.split(':')

Farrel

> **···**
>
> On 19/12/2007, Peter Loftus \<loftuz@gmail.com\> wrote:
> 
> > Hey guys so i have a example of splitting elements in an array and  
> > getting the third word. Im using spaces to split it. Anyone know how to  
> > split it if i need to use : instead of spaces
> > 
> > arr = File.readlines("sample.txt")  
> > arr.map! {|line| line.split("\s")[2]}
