On 6/6/2011 4:46 PM, Stanislaw Romanski wrote:
> Hi,
> Is it possible to write a function having an aarray (not: a reference to 
> array)
> as a parameter ?
> ------------------------------------------------------------------------------
> For example, *the *first parameter of *splice* function is an array.
> my @taborig = ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
> my @rslt = splice( @taborig, 2, 3);
> print Dumper(\@rslt); # e2 e3 e4
> @taborig = ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
> @rslt = splice( @taborig, 2);
> print Dumper(\@rslt); # e2 e3 e4 e5 e6
> Can I write a function 'my_splice' acting the same way ?

Of course, except it will then be multiple parameters (one
for each element of the array) instead of one parameter.

I'm surprised you didn't just experiment and see what happens. :)

EG:

my @result = my_splice (@taborig);

sub my_splice {
        my @array = @_;
}

It's usually simpler to just use a reference though.

my @result = my_splice (\@taborig);

sub my_splice {
        my $aref = @_;
}

If you like, you can immediately turn it back into an array

        my @array = @$aref;

or just deref the ref in the sub when you use it :

        @rslt = splice @$aref, ...

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to