how do I pass arrays from mainscript into the subroutine

--0-41195570-1304783252=:98777
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Hi,=0ALooking at the script below, how do I pass 2 arrays from the main scr=
ipt into =0Athe subroutine? Thanks=0A=0A#### script ######=0A#!/usr/bin/per=
l=0Ause strict;=0Amy [at] whatever =3D qw(a b c d e);=0Amy [at] test =3D qw(1 2 3 4=
5 6 7);=0A{=0A=A0sub testing {=0A=A0=A0 my ( [at] data1 , [at] data2)=A0 =3D [at] _;=0A=
=A0=A0 print "\ [at] data1 =3D [at] data1\n";=0A=A0=A0 print "\ [at] data2 =3D [at] data2\n";=
=0A=A0}=0A}=0A&testing ( [at] whatever , [at] test);
--0-41195570-1304783252=:98777--
eventual [ Sa, 07 Mai 2011 17:47 ] [ ID #2059255 ]

Re: how do I pass arrays from mainscript into the subroutine

On 11-05-07 11:47 AM, eventual wrote:
> Hi,
> Looking at the script below, how do I pass 2 arrays from the main script into
> the subroutine? Thanks
>
> #### script ######
> #!/usr/bin/perl
> use strict;
> my [at] whatever = qw(a b c d e);
> my [at] test = qw(1 2 3 4 5 6 7);
> {
> sub testing {
> my ( [at] data1 , [at] data2) = [at] _;
> print "\ [at] data1 = [at] data1\n";
> print "\ [at] data2 = [at] data2\n";
> }
> }
> &testing ( [at] whatever , [at] test);

You have to use references to the arrays:

#!/usr/bin/env perl

use strict;
use warnings;

my [at] whatever = qw(a b c d e);
my [at] test = qw(1 2 3 4 5 6 7);

sub testing {
my [at] data1 = [at] { $_[0] };
my [at] data2 = [at] { $_[1] };

print "\ [at] data1 = [at] data1\n";
print "\ [at] data2 = [at] data2\n";
}

testing( \ [at] whatever, \ [at] test );

__END__

Also, don't call a sub with the &. Does can do unexpected things.

See:
perldoc perlreftut
perldoc perlref


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shawn H Corey [ Sa, 07 Mai 2011 17:59 ] [ ID #2059256 ]

Re: how do I pass arrays from mainscript into the subroutine

>>>>> "SHC" == Shawn H Corey <shawnhcorey [at] ncf.ca> writes:


SHC> #!/usr/bin/env perl

that is not a good trick to run perl. also it forks off an extra process
which slows things down.

SHC> use strict;
SHC> use warnings;

SHC> my [at] whatever = qw(a b c d e);
SHC> my [at] test = qw(1 2 3 4 5 6 7);

SHC> sub testing {
SHC> my [at] data1 = [at] { $_[0] };
SHC> my [at] data2 = [at] { $_[1] };

why do you need to copy the arrays over? keep them as refs and
dereference them as needed.

my( $data1, $data2 ) = [at] _ ;

SHC> print "\ [at] data1 = [at] data1\n";
SHC> print "\ [at] data2 = [at] data2\n";
SHC> }

print "data1 = [at] {$data1}\n";
print "data2 = [at] {$data2}\n";

SHC> See:
SHC> perldoc perlreftut
SHC> perldoc perlref

those should be mentioned more prominently and earlier. obviously the OP
doesn't know about refs so they need to learn the basics.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Sa, 07 Mai 2011 18:23 ] [ ID #2059257 ]

Re: how do I pass arrays from mainscript into the subroutine

>
> Also, don't call a sub with the &. =A0Does can do unexpected things.
>

Just for my understanding, what are the other issues with &sub apart
from skipping prototype definitions?

Thanks,
Sandip

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sandip Bhattacharya [ Mo, 09 Mai 2011 17:03 ] [ ID #2059292 ]

Re: how do I pass arrays from mainscript into the subroutine

On 5/9/11 Mon May 9, 2011 8:03 AM, "Sandip Bhattacharya"
<sandipb [at] foss-community.com> scribbled:

>>
>> Also, don't call a sub with the &. =A0Does can do unexpected things.
>>
>
> Just for my understanding, what are the other issues with &sub apart
> from skipping prototype definitions?

Please read the descriptions in 'perldoc perlsub'. Search for "Subroutines
may be called recursively' and the section titled 'Prototypes'.
Specifically, the use of the &sub form means that prototype checking will
not be done and, if no arguments are included in the subroutine call, the [at] =
_
array will not be initialized and will be whatever value it had at the time
of the call.

While the use of subroutine prototypes is rare and the absence of checking
not often a problem, having an unexpected value in [at] _ could cause unintende=
d
results.



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Mo, 09 Mai 2011 17:51 ] [ ID #2059293 ]
Perl » gmane.comp.lang.perl.beginners » how do I pass arrays from mainscript into the subroutine

Vorheriges Thema: Help with regular expressions
Nächstes Thema: read the content of a hidden folder in a list