# Slicing arrays

**URL:** <https://www.ruby-forum.com/t/slicing-arrays/95587>\
**Category:** Ruby\
**Created:** [June 6, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/slicing-arrays/95587 "2006-06-06T21:42:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [June 6, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/slicing-arrays/95587/1 "2006-06-06T21:42:19Z")

</div>

if have a text file I am parsing. I am using split to break the line  
into an  
array.  
I want to then use splice to pick and choose the fields that I one.

so for example i have a string.  
mystring = ‘1,2,3,4,5,6’

then i want to get an array but I only want the 2 field and the last  
three  
fields.  
myarray = mystring.split(’,’).splice(1,3…5)

This breaks.

I can get the range 3…5 or i get the element splice(1).

Am I missing something?

Do i just build the array as normal and then make a new array from the  
elements I need?

Paul

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [June 6, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/slicing-arrays/95587/2 "2006-06-06T23:11:45Z")

</div>

On 6/6/06, Paul D. Kraus [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> fields.  
> myarray = mystring.split(‘,’).splice(1,3…5)

Honestly I do not know about a splice method for Arrays

here is a less elegant way

```
m = mystring.split(',')
m[1..1] + m[3..5]

```

or in case you want to specify your ranges in a string  
“1,3…5”.split(“,”).map{|x| x.split(“…”)}.flatten.map{|x| m[x.to\_i]}

or you write your own splice method

class Array  
def splice( \*args )  
args.map{ |arg|  
case arg  
when Integer  
arg  
when Range  
arg.to\_a  
else  
raise TypeError, “blah #{arg} is not to our liking”  
end # case arg  
}.flatten.map{ |a| self[a] }  
end # def splice( \*args )  
end # class Array

well now I know one 😉  
Hope that helps  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [June 6, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/slicing-arrays/95587/3 "2006-06-06T23:24:22Z")

</div>

Paul D. Kraus wrote:

> fields.
> 
> Paul

Have you tried the values\_at method?

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [June 6, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/slicing-arrays/95587/4 "2006-06-06T23:45:58Z")

</div>

> Have you tried the values\_at method?

Arrgh I normally always read the doc, sorry OP forget about my splice it  
is  
already here

values\_at

great job!

Robert
