[comp.lang.perl] Perl-Users Digest #96

aks@hub.ucsb.edu, , ks (Alan Stebbens) (02/27/90)

| What we need is some equivalent to the substr() function when
| used as an lvalue--if the thing you assign is the same len> gth,
| it changes in place; if it's longer, the array grows, and if
| it's shorter, the array shrinks.  Assignment to a slice would be
| a reasonable syntax, but that already means something different,
| alas.  And what would

| 	$elt = pop(@array[1,8,4])

| mean?

| Perhaps

| 	subary(@array, $n, 0) = ($elt1, $elt2, ...);    # your push

| 	$elt = $array[$n];				# your pop
| 	subary(@array, $n, 1) = ();

| Open to suggestions.  Larry

Why not:

  insert(@array,$index,$e1, $e2, ...);	# to insert

and

  remove(@array, $index, $length);	# to remove

The "insert" function inserts new elements into an array, and
returns the new array's lvalue.  The following equivalencies hold:
	
  @a = (5,8);
  insert(@a,$[,1,2,3) == (1,2,3,5,8);

  @a = (1,2,3);
  insert(@a,$#a,5,8) == (1,2,3,5,8);

  @a = (1,2,8);
  insert(@a,$[+2,3,5) == (1,2,3,5,8);

Notice that "insert(@a,$[,elts...)" is functionally equivalent to
"unshift(@a,elts)", although the former is more intuitive.
"Insert(@a,$#a,elts)" is functionally equivalent to
"push(@a,elts)".

The "remove" function removes the indicated elements from the
array, and returns them as the result.  Examples:

  @a = (1,2,3,5,8);
  remove(@a,$[,2) == (3,5,8);

  @a = (1,2,3,5,8);
  remove(@a,$#a-1,2) == (1,2,3);

  @a = (1,2,3,5,8);
  remove(@a,$[+2,2) == (1,2,8);

Alan Stebbens        <aks@hub.ucsb.edu>             (805) 961-3221
     Center for Computational Sciences and Engineering (CCSE)
          University of California, Santa Barbara (UCSB)
           3111 Engineering I, Santa Barbara, CA 93106