From: Robert Dober Date: 2006-06-07T06:08:49+09:00 Subject: Re: Slicing arrays ------=_Part_52867_26843835.1149628125832 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 6/6/06, Paul D. Kraus wrote: > > 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 =3D '1,2,3,4,5,6' > > then i want to get an array but I only want the 2 field and the last thre= e > fields. > myarray =3D mystring.split(',').splice(1,3..5) Honestly I do not know about a splice method for Arrays here is a less elegant way m =3D 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 --=20 Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_52867_26843835.1149628125832--