Array as function parameter
Array as function parameter
am 07.06.2011 01:46:44 von Stanislaw Romanski
This is a multi-part message in MIME format.
--===============1902341364==
Content-Type: multipart/alternative;
boundary="----=_NextPart_000_0088_01CC24B4.C12B5EE0"
This is a multi-part message in MIME format.
------=_NextPart_000_0088_01CC24B4.C12B5EE0
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
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 =3D ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
my @rslt =3D splice( @taborig, 2, 3);
print Dumper(\@rslt); # e2 e3 e4
@taborig =3D ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
@rslt =3D splice( @taborig, 2);
print Dumper(\@rslt); # e2 e3 e4 e5 e6=20
Can I write a function 'my_splice' acting the same way ?
Cheers,
StanisÅaw RomaÅski
------=_NextPart_000_0088_01CC24B4.C12B5EE0
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
ï»=BF
Hi,
Is it possible to write a function =
having an=20
aarray (not: a reference to array)
as a parameter ?
size=3D2>--------------------------------------------------- -------------=
--------------
For example, the =
first=20
parameter of splice function is an =
array.
=
my=20
@taborig =3D ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
face=3DArial=20
size=3D2> my @rslt =3D =
splice( @taborig,=20
2, 3);
print=20
Dumper(\@rslt); # e2 e3 e4
size=3D2> @taborig =
=3D ( qw( e0=20
e1 e2 e3 e4 e5 e6 ) );
size=3D2> @rslt =3D =
splice(=20
@taborig, 2);
print=20
Dumper(\@rslt); # e2 e3 e4 e5 e6
Can I write a function 'my_splice' =
acting the same=20
way ?
Cheers,
StanisÅaw RomaÅski
------=_NextPart_000_0088_01CC24B4.C12B5EE0--
--===============1902341364==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1902341364==--
Re: Array as function parameter
am 07.06.2011 02:28:16 von Bill Luebkert
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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Array as function parameter
am 07.06.2011 03:00:44 von Stanislaw Romanski
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"
To: "Stanislaw Romanski"
Cc:
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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs