Array as function parameter

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 [at] taborig =3D ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
my [at] rslt =3D splice( [at] taborig, 2, 3);
print Dumper(\ [at] rslt); # e2 e3 e4

[at] taborig =3D ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
[at] rslt =3D splice( [at] taborig, 2);
print Dumper(\ [at] rslt); # e2 e3 e4 e5 e6

Can I write a function 'my_splice' acting the same way ?

Cheers,

Stanis=C5=82aw Roma=C5=84ski

------=_NextPart_000_0088_01CC24B4.C12B5EE0
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable

=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
<META content=3D"MSHTML 6.00.6000.17097" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Is it possible to write a function =
having an
aarray (not: a reference to array)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>as a parameter ?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial
size=3D2>--------------------------------------------------- -------------=
--------------</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>For example, <STRONG>the =
</STRONG>first
parameter of <STRONG>splice</STRONG> function is an =
array.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>        =
 my
[at] taborig =3D ( qw( e0 e1 e2 e3 e4 e5 e6 ) );<BR></FONT><FONT =
face=3DArial
size=3D2>         my [at] rslt =3D =
splice( [at] taborig,
2, 3);<BR>         print
Dumper(\ [at] rslt);   # e2 e3 e4</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial
size=3D2>          [at] taborig =
=3D ( qw( e0
e1 e2 e3 e4 e5 e6 ) );<BR></FONT><FONT face=3DArial
size=3D2>          [at] rslt =3D =
splice(
[at] taborig, 2);<BR>         print
Dumper(\ [at] rslt);   # e2 e3 e4 e5 e6 </FONT></DIV>
<DIV> </DIV></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Can I write a function 'my_splice' =
acting the same
way ?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Cheers,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> </DIV>
<DIV>Stanis=C5=82aw Roma=C5=84ski<BR></DIV></FONT></BODY></HTML>

------=_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 [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1902341364==--
Stanislaw Romanski [ Di, 07 Juni 2011 01:46 ] [ ID #2060616 ]

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 [at] taborig = ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
> my [at] rslt = splice( [at] taborig, 2, 3);
> print Dumper(\ [at] rslt); # e2 e3 e4
> [at] taborig = ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
> [at] rslt = splice( [at] taborig, 2);
> print Dumper(\ [at] 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 [at] result = my_splice ( [at] taborig);

sub my_splice {
my [at] array = [at] _;
}

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

my [at] result = my_splice (\ [at] taborig);

sub my_splice {
my $aref = [at] _;
}

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

my [at] array = [at] $aref;

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

[at] rslt = splice [at] $aref, ...

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Bill Luebkert [ Di, 07 Juni 2011 02:28 ] [ ID #2060617 ]

Re: Array as function parameter

Hello,

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

sub proto_fun ( \ [at] $;$ )
{
my ($x, $y, $z) = [at] _;
print Dumper('proto_fun',$x, $y, $z);
}
[at] rslt = proto_fun( [at] taborig, 2, 3);
[at] rslt = proto_fun( [at] 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" <dbecoll [at] roadrunner.com>
To: "Stanislaw Romanski" <s.romanski [at] datos.pl>
Cc: <activeperl [at] listserv.ActiveState.com>
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 [at] taborig = ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
>> my [at] rslt = splice( [at] taborig, 2, 3);
>> print Dumper(\ [at] rslt); # e2 e3 e4
>> [at] taborig = ( qw( e0 e1 e2 e3 e4 e5 e6 ) );
>> [at] rslt = splice( [at] taborig, 2);
>> print Dumper(\ [at] 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 [at] result = my_splice ( [at] taborig);
>
> sub my_splice {
> my [at] array = [at] _;
> }
>
> It's usually simpler to just use a reference though.
>
> my [at] result = my_splice (\ [at] taborig);
>
> sub my_splice {
> my $aref = [at] _;
> }
>
> If you like, you can immediately turn it back into an array
>
> my [at] array = [at] $aref;
>
> or just deref the ref in the sub when you use it :
>
> [at] rslt = splice [at] $aref, ...
>

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Stanislaw Romanski [ Di, 07 Juni 2011 03:00 ] [ ID #2060618 ]
Perl » gmane.comp.lang.perl.active-perl » Array as function parameter

Vorheriges Thema: ANNOUNCE: ActivePerl 5.8 and 5.10 Community Edition - end of support
Nächstes Thema: How to make LWP ignore ssl warnings