Hello,

The solution to my question is to use a 'prototyped' function.

    sub proto_fun ( \@$;$ )
    {
            my ($x, $y, $z) = @_;
            print Dumper('proto_fun',$x, $y, $z);
    }
    @rslt = proto_fun( @taborig, 2, 3);
    @rslt = proto_fun( @taborig, 2);

Inside 'proto_fun'
- $x is a reference to the array
- $y is a value of the second actual parameter
- $z is a value of the third actual parameter (it is optional - 'undef' if 
not given)

Thanks for the help
(specially to Dominik Jarmulowicz, who gave me the hint )

Stanislaw Romanski

----- Original Message ----- 
From: "Bill Luebkert" <[email protected]>
To: "Stanislaw Romanski" <[email protected]>
Cc: <[email protected]>
Sent: Tuesday, June 07, 2011 2:28 AM
Subject: Re: Array as function parameter


> 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