SPUG: Splice

John W. Krahn krahnj at telus.net
Thu Nov 4 14:06:55 CST 2004


Dan Ebert wrote:
> OK, this is an easy one.  I just need a sanity check.
> 
> splice(@array,0);
> 
> does the same thing as
> 
> @array = ();
> 
> right?

Not quite.

$ perl -le'@x = qw/a b c d e/; @y = splice @x, 0; print "\@x = " . @x . " 
\@y = " . @y'
@x = 0   @y = 5
$ perl -le'@x = qw/a b c d e/; @y = @x = (); print "\@x = " . @x . "   \@y = " 
. @y'
@x = 0   @y = 0

splice returns the contents of the spliced array while assignment returns the 
result of the assignment.

BTW, the following all do the same thing:

splice @array;
splice @array, 0;
splice @array, 0, @array;
splice @array, 0, @array, ();



John
-- 
use Perl;
program
fulfillment


More information about the spug-list mailing list