how do I pass arrays from mainscript into the subroutine
how do I pass arrays from mainscript into the subroutine
am 07.05.2011 17:47:32 von eventual
--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
#### script ######=0A#!/usr/bin/per=
l=0Ause strict;=0Amy @whatever =3D qw(a b c d e);=0Amy @test =3D qw(1 2 3 4=
5 6 7);=0A{
sub testing {
=A0 my (@data1 , @data2)=A0 =3D @_;=0A=
print "\@data1 =3D @data1\n";
=A0 print "\@data2 =3D @data2\n";=
}=0A}=0A&testing (@whatever ,@test);
--0-41195570-1304783252=:98777--
Re: how do I pass arrays from mainscript into the subroutine
am 07.05.2011 17:59:23 von Shawn H Corey
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 @whatever = qw(a b c d e);
> my @test = qw(1 2 3 4 5 6 7);
> {
> sub testing {
> my (@data1 , @data2) = @_;
> print "\@data1 = @data1\n";
> print "\@data2 = @data2\n";
> }
> }
> &testing (@whatever ,@test);
You have to use references to the arrays:
#!/usr/bin/env perl
use strict;
use warnings;
my @whatever = qw(a b c d e);
my @test = qw(1 2 3 4 5 6 7);
sub testing {
my @data1 = @{ $_[0] };
my @data2 = @{ $_[1] };
print "\@data1 = @data1\n";
print "\@data2 = @data2\n";
}
testing( \@whatever, \@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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how do I pass arrays from mainscript into the subroutine
am 07.05.2011 18:23:42 von Uri Guttman
>>>>> "SHC" == Shawn H Corey 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 @whatever = qw(a b c d e);
SHC> my @test = qw(1 2 3 4 5 6 7);
SHC> sub testing {
SHC> my @data1 = @{ $_[0] };
SHC> my @data2 = @{ $_[1] };
why do you need to copy the arrays over? keep them as refs and
dereference them as needed.
my( $data1, $data2 ) = @_ ;
SHC> print "\@data1 = @data1\n";
SHC> print "\@data2 = @data2\n";
SHC> }
print "data1 = @{$data1}\n";
print "data2 = @{$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@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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how do I pass arrays from mainscript into the subroutine
am 09.05.2011 17:03:09 von Sandip Bhattacharya
>
> 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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: how do I pass arrays from mainscript into the subroutine
am 09.05.2011 17:51:03 von Jim Gibson
On 5/9/11 Mon May 9, 2011 8:03 AM, "Sandip Bhattacharya"
scribbled:
>>=20
>> Also, don't call a sub with the &. =A0Does can do unexpected things.
>>=20
>=20
> 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 @=
_
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 @_ could cause unintende=
d
results.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/