CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Passing arrays to subroutines



Aug 18, 2009, 4:05 AM

Post #1 of 4 (1434 views)
Passing arrays to subroutines Can't Post

Hi Everyone,

I have a script that passes three arrays into a subroutine held in a perl module ( I have created:

PM1::Process_Data_File(\@_array1, \@_array2, \@_array3);

In the arguments are handled as follows:

my($_array1, $_array2, $_array3) = @_;


What I want to know is how I go on from this Module and pass these to another module with the subroutine Further_Processing so I can use them again. I am unsure what to do because the arrays have been flattened.

PM2::Further_Processing(?, ?, ?);


(This post was edited by andy_wood on Aug 18, 2009, 4:07 AM)


Aug 18, 2009, 5:06 AM

Post #2 of 4 (1421 views)
Re: [andy_wood] Passing arrays to subroutines [In reply to] Can't Post

I'm not sure I understand your question completely, but I will try to answer anyway.
First, just want to specify that you don't passes 3 arrays to the first subroutine, but the refferences to the 3 arrays.
This it seems like you do correctly, since you store them the refferences in scalars in the Process_data_File subroutine.

I don't understand what you mean when you are saying that the arrays have been flattened.

If you would like to transfer them to another subroutine, you can just do this in the same way.

Further_Processing($_array1,$_array2,$_array3) ;

I suspect that your problem is that you don't completly understand what you are doing with the refferences.
If that is the case I recomend you to read the refference tutorial from perldoc


Aug 18, 2009, 9:02 AM

Post #3 of 4 (1385 views)
Re: [andy_wood] Passing arrays to subroutines [In reply to] Can't Post

since you are passing the arrays by reference, the arrays are not flattened.

New User

Aug 21, 2009, 12:28 PM

Post #4 of 4 (1354 views)
Re: [andy_wood] Passing arrays to subroutines [In reply to] Can't Post

$_array1 etc contain the references. To get the originals, dereference :

my @array=(1,2,3); 

sub refcheck(\@){
my $_array1=@_[0];
my @_array1 = @{$_array1};
print $_array1; #gives something like ARRAY(0x24bfe8c)
print @_array1; # gives 123

To pass them on, use the references again, like spider said.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives