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 an array to a module

 



bnfo_equestrian
New User


Jul 29, 2009, 10:58 AM

Post #1 of 4 (531 views)
Passing an array to a module Can't Post

Hey, first post here!

I am stuck on how to pass a 2D array to a subroutine in a module I have written. All I can get out of it is the reference to the array and my dereferencing doesn't seem to be working.


Code
  

In the .pl file

#I have a loop here{

push @tags_in_TAC, [$id_TAC,$chromosome_TAC,$id_tag,$sequence_tag,$start_tag,$orientation_tag,$error_tag];

}

calculate_depth_TAC($start_TAC,$end_TAC,@tags_in_TAC); #pass two scalars and my array to the module subroutine

In the .pm file

sub calculate_depth_TAC{

($TAC_start, $TAC_end, @tag_array) = @_;

for($i=$TAC_start;$i<=$TAC_end;$i++){
foreach $element(@$tag_array){
print "Tagarray = $tag_array\n"; #All this gives me is the reference to the array, even though I dereferenced it. Help?

}

}





Thanks Smile
No hour of life is wasted that is spent in the saddle. ~Winston Churchill


KevinR
Veteran


Jul 29, 2009, 12:06 PM

Post #2 of 4 (528 views)
Re: [bnfo_equestrian] Passing an array to a module [In reply to] Can't Post

You pass a multi-dimensional array by reference:


Code
push @tags_in_TAC, [$id_TAC,$chromosome_TAC,$id_tag,$sequence_tag,$start_tag,$orientation_tag,$error_tag];  

calculate_depth_TAC($start_TAC,$end_TAC,\@tags_in_TAC); #pass two scalars and my array to the module subroutine


sub calculate_depth_TAC{

($TAC_start, $TAC_end, $tag_array) = @_;

foreach my $array (@{$tag_array}){
foreach $element (@{$array}){
print "Tagarray = $element\n"; #All this gives me is the reference to the array, even though I dereferenced it. Help?

}
}
}



I don't have the energy to explain all the code but will provide you some links to read up on data structures and references. The first three tutorials on this page discuss them:

http://perldoc.perl.org/index-tutorials.html

On another topic..... you should start using these three pragmas now:

use strict;
use warnings;
use diagnostics;

the last one can be dropped later but for now use it as it will give more detailed information about warnings and I think also errors.
-------------------------------------------------


bnfo_equestrian
New User


Jul 29, 2009, 12:10 PM

Post #3 of 4 (526 views)
Re: [KevinR] Passing an array to a module [In reply to] Can't Post

Thanks, this actually answers my question!

I know pass by ref and pass by value and I know I wasn't doing it right. I just cant ever remember the syntax. Thanks for your help.

PS I do use strict and warnings Smile
No hour of life is wasted that is spent in the saddle. ~Winston Churchill


KevinR
Veteran


Jul 29, 2009, 12:25 PM

Post #4 of 4 (524 views)
Re: [bnfo_equestrian] Passing an array to a module [In reply to] Can't Post


In Reply To
Thanks, this actually answers my question!

I know pass by ref and pass by value and I know I wasn't doing it right. I just cant ever remember the syntax. Thanks for your help.

PS I do use strict and warnings Smile


Very good. You get 3.5 stars out of a possible 5. Wink
-------------------------------------------------

 
 


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

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