From: Ben Tilly Date: 2000-12-10T15:33:34+09:00 Subject: [ruby-talk:6999] Re: Accessing an non-existing segment of an array Dave Thomas wrote: > >"Ben Tilly" writes: > > > >I guess my confusion comes from the intent of the code: why would you > > >ever _ask_ for elements outside the bounds of the array? > > > > > I have felt the convenience in Perl. For instance: > > > > while (my @group = splice(@list, 0, 10)) { > > # Do something with the group of entries > > } > > > > The point being to push down to the language all of the > > logic about where the boundaries of the array are. This > > snippet for implementing paging logic would be much longer > > if I had to concern myself with logic about finding the > > boundary of the array. Instead I just know that it will > > exit correctly, and might sometimes give me fewer elements. > >I guess in this circumstance I'd be looking for an extension to #pop >or #unshift > > arr.pop(10) -> newArray > >where newArray would be nil if arr was empty, or up to 10 elements >otherwise. You mean #pop or #shift? Anyways that is what splice in Perl is. An extension and generalization of pop, shift, unshift and push. All rolled up in one. Anywhere else in Perl trying to access stuff beyond array bounds automatically results in undef values being created. But splice stands as a lone (and sometimes useful) exception. >But I can see your point too. FWIW this is the only place that I know of where this seems useful to me. Quite literally I was staring at some code and said, "Wouldn't it be nice if splice did this so I could eliminate this logic?" Then found that it worked that way already. :-) Cheers, Ben _____________________________________________________________________________________ Get more from the Web. FREE MSN Explorer download : http://explorer.msn.com