From: Daniel Berger Date: 2006-10-05T00:10:13+09:00 Subject: Re: Equvialent of Python slices? Logan Capaldo wrote: > On Wed, Oct 04, 2006 at 11:10:10PM +0900, Daniel Berger wrote: > > > > James Edward Gray II wrote: > > > On Oct 4, 2006, at 8:19 AM, Peter Szinek wrote: > > > > > > > Hello, > > > > > > > > Sorry for the lame question again, but even after a lot of googling > > > > I still couldn't find the $subj., ie. something like: > > > > > > > > >>> "asdfghj"[2::2] > > > > 'dgj' > > > > (= every second letter, beginning from the 2nd index in the string) > > > > Interesting, though I'm surprised Python has explicit syntax for what > > seems like a highly specific type of operation. But, hey, cool. > > > > > I'm not going to call this as pretty, of course, but: > > > > > > >> test = "asdfghj" > > > => "asdfghj" > > > >> test[2..-1].gsub(/(.)./, "\\1") > > > => "dgj" > > > > Slightly OT here.... > > > > I'm trying to remember why test[2, -1] returns nil. Positive numbers > > for the second integer work as expected: > > > > test[2,1] => 'd' > > test[2,2] => 'df' > > test[2,100] => dfghj > > test[2,-1] => nil > I can see it logically, test[start position, length]. A negative one > length doesn't make much sense. test[2..-1] works though, since it's > start postion..(.)end position. Unfortunately you can't coun't backwards > with a negative length ( like if I want the last two characters I can't > say test[-1, -2]. ) Ok, I just need to remember the second value is a length, not an index, although that still confuses me. I wonder if making that change would cause havoc with the regex engine. Best to leave it alone I suppose. Regards, Dan