Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing an array as a parameter 1

Status
Not open for further replies.

jimberger

Programmer
Joined
Jul 5, 2001
Messages
222
Location
GB
hi all,

i am trying to pass an array as a paramater to a sub routine as follows:

&PrintYearlyReports(@references);

i am accessing the paramater in the sub routine as follows:

my @references = @_[0];

but when i print the array out it just gives me the first value of the array
any ideas?

thanks
 
Its the [0]. That is only the first element. You can just use @_. Look below.

use strict;

my @array=('a','b','c','d');
&caps(@array);

sub caps{
my @newarray=@_;
foreach my $element(@newarray){
print uc($element);
print "\n";
}
}

Mike
 
Jim,
I want to add something else to the code I just posted. If you don't want to deal with passing arrays, just pass a reference to it.

use strict;

my @array=('a','b','c','d');
&caps(\@array); #pass by reference to the array

sub caps{
my ($arrayref)=@_;#ref the array
my @newarray=@{$arrayref};#dereference the array
foreach my $element(@newarray){
print uc($element);
print "\n";
}
}

NOTE:
1. my ($arrayref)=@_;#ref the array
The line above can be done this way too:
2. my $arrayref=shift;

Line 1 takes from an array so it must create a list.
Line 2 grabs a scalar.

Mike
 
When you pass an entire array as a parameter, the array gets flattened out as a long parameter list to the subroutine - the subroutine will receive one parameter for each element of the array.

The method I always use is to pass arrays and hashes to subroutines as *references* - each array or hash can be passed with one scalar reference. See the FAQ in this forum for passing references to subroutines.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
All of those responses were good. I use both method, depending on what I need to do, and the parameters being passed. If all you are passing is an array, it's probably easier to just pass it. If you need to pass more than one parameter and one or more is an array, you'll be better off passing references. But references can be confusing to some people. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
which method is best for performance and efficiency though?

 
Passing the array reference is probably more efficient, since perl doesn't have to convert the array to a list of parameters, pass those to the function, and reasssing them inside the function.

Another thing: when you pass the array as a list, changing values of the parameters in your function will NOT affect the original array, since perl is passing a COPY of it. But when you pass a reference to the array, you can access and modify the original array. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top