[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Perl's splice function, a newbie query



At 19.06 -0500 1998.11.08, Steve Doonan wrote:
>I am a Perl newbie. I have been puzzled by several features
>of this function. Could someone please confirm or refute my
>impressions, listed below:

Well, one thing about programming (and scripting languages especially, and
an evolved, quirky language like Perl even moreso) is that it is somewhat
of an empirical science.  Try it out.  In MacPerl it is very easy to do so.

I'll set the array as:

  @a = 0..9;

Then I will do a splice, and then print the remaining array as:

  print join ',', @a;

>* If LENGTH is 0 (zero), the function removes nothing.

  splice(@a, 2, 0);

>* OFFSET can be either a positive or negative number
>  (such as 1 or -2);

  splice(@a, -2, 2);

>* LENGTH can be only a positive number
>  (1 or 2 or 3, etc.);

  splice(@a, 2, -2);

>* OFFSET does NOT refer to a particular subscript of the
>  specified array (an offset of 3 does NOT refer to the
>  fourth element of a zero-base-indexed array);

  splice(@a, 3, 1);

>* OFFSET does NOT refer to a particular element of the
>  specified array (specifying an offset of 3 does NOT
>  refer to the third element of the array);

  splice(@a, 3, 1);

>* OFFSET DOES refer to the "nothing" BETWEEN two elements
>  of the specified array (Is this true?);

  splice(@a, 3, 1);

>* an OFFSET of 0 (zero) refers to the "nothing" before
>  the first element of the specified array, and an OFFSET
>  of -1 refers to the "nothing" between the last two elements
>  of the specified array.

  splice(@a, 3, 1);

Your assumptions are pretty much accurate.  But if you try these out, you
should know that.  :)

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch