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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



andy_wood
Novice

Aug 18, 2009, 4:05 AM

Post #1 of 4 (741 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 (PM1.pm) I have created:

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



In PM1.pm the arguments are handled as follows:

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

....



What I want to know is how I go on from this Module PM1.pm and pass these to another module PM2.pm 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(?, ?, ?);

Thanks,


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


spider
User


Aug 18, 2009, 5:06 AM

Post #2 of 4 (728 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.



Code
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 http://perldoc.perl.org/perlreftut.html.


KevinR
Veteran


Aug 18, 2009, 9:02 AM

Post #3 of 4 (692 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.
-------------------------------------------------


Jolle
New User

Aug 21, 2009, 12:28 PM

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

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

Code
my @array=(1,2,3); 
refcheck(\@array);

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