Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Are only simple scalars allowed in "do SUBROUTINE (LIST)" ? Message-ID: <7320@jpl-devvax.JPL.NASA.GOV> Date: 7 Mar 90 19:16:46 GMT References: <1211@frankland-river.aaii.oz.au> <7269@jpl-devvax.JPL.NASA.GOV> <21611@watdragon.waterloo.edu> <1990Mar6.180823.27618@iwarp.intel.com> <7294@jpl-devvax.JPL.NASA.GOV> <15236@bfmny0.UU.NET> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 54 In article <15236@bfmny0.UU.NET> tneff@bfmny0.UU.NET (Tom Neff) writes: : In article <7294@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes: : >Would you settle for this? : > : > ( LIST ) [ EXPR ] : : Definitely, absolutely, this would be great!! : : Slicing self-defining lists would be a subtle and powerful technique if : implemented. And it's an elegant fit in the Perl model. It's already implemented, and will be in patch 13. Wasn't too hard--just had to get the do_slice() routine to allow the stack as a valid array to fetch things from. An interesting side effect of this is that negative subscripts refer to items farther down the stack. I'm not going to document this, but I've made (LIST)[-1] (assuming $[ is 0) be the size of the stack underneath (LIST)[-1], so (LIST)[-2] is the top of the stack before this expression started being evaluated. Might turn out handy for some debugging applications. If you write a script depending on this, we will deny your existence... I'm also doing the other array operator we talked about as follows splice(@array,$offset,$length,LIST); It always chops the elements mentioned out the array and returns them. It then replaces those elements with the elements of LIST, which can of course be a null list. The following equivalencies hold: pop(@array) splice(@array,-1,1) shift(@array) splice(@array,0,1) push(@array,$x,$y) splice(@array,$#array+1,0,$x,$y) unshift(@array,$x,$y) splice(@array,0,0,$x,$y) Implementation-wise, this will be the same as assigning to substr()--if you insert a LIST the same size as the one you delete, nothing has to be moved. If you shrink it, it moves the minimal number of pointers to satisfy it. If you grow it, it tries to do the same, but it might have to realloc the whole shmear. Such is life. I thought "splice" was the absolutely perfect name for this. I had considered doing splice(@array,$offset,$length) = ($x,$y) but if I did that I'd either have to not return the old value consistently, or I'd have to violate the rule that assignment always returns the new value of a thing. Besides, lvalues are hard. So splice syntax just turns out to be a variant of push. I didn't even have to add to the grammar for it.. Larry