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: Beginner:
Passing two arrays to a subroutine

 



tej.87g
Novice

Sep 27, 2010, 3:36 AM

Post #1 of 3 (1136 views)
Passing two arrays to a subroutine Can't Post

I am trying to pass two different arrays to subroutine in same file. First array @act_table is two dimentional array and another is simple array (@colm)Below is my code : for ($h=1; $h<=$arg[1]; $h++) { my @colm; for ($t=1;$t<=$arg[2];$t++) { push @colm, $act_table[$t][$h]; } Column(@act_table,@colm); } for ($i=1; $i<=$arg[2]; $i++) { for ($n=1;$n<=$arg[1];$n++) { #print" $table_arr[$i][$n]"; } }sub Column { no warnings; my @actcol=@_; my @col=@_; $colno++; print "In column : $colno\n"; foreach $_ (@col) { print "$_\n "; $_=~s/(<Tc(?:\s?\;?\s?\;?\d?)?>)(.*)/Entity_convert($2)/eg; $_= ThinSpace($_); #print "***************** $_\n"; }}

When i print @col trough foreach loop i get garbage values first and then actual values

This is really urgent. Can anybody please tell me what going wrong in my code?Thanks in advance


7stud
Enthusiast

Sep 27, 2010, 4:49 AM

Post #2 of 3 (1135 views)
Re: [tej.87g] Passing two arrays to a subroutine [In reply to] Can't Post

lol


rameshg87
Novice

Sep 27, 2010, 9:21 AM

Post #3 of 3 (1123 views)
Re: [tej.87g] Passing two arrays to a subroutine [In reply to] Can't Post

 
First of all, please use the code feature when you post your messages. This will help you to format the code and help others understand. This is how your code will look like with proper formatting


Code
 
for ($h=1; $h<=$arg[1]; $h++) {
my @colm;
for ($t=1;$t<=$arg[2];$t++) {
push @colm, $act_table[$t][$h];
}
Column(@act_table,@colm);
}

for ($i=1; $i<=$arg[2]; $i++) {
for ($n=1;$n<=$arg[1];$n++) {
#print" $table_arr[$i][$n]";
}
}

sub Column {

no warnings;
my @actcol=@_;
my @col=@_;

$colno++;
print "In column : $colno\n";
foreach $_ (@col) {
print "$_\n ";
$_=~s/(<Tc(?:\s?\;?\s?\;?\d?)?>)(.*)/Entity_convert($2)/eg;
$_= ThinSpace($_);
#print "***************** $_\n";
}
}



The main problem is how you have passed two arrays to the routine. Perl knows only a single array @_ passed to the routine. If you want to pass two arrays into the same routine, you need to pass their references. I haven't checked the logistics of the code but first it requires these modifications to work



Code
    Column(\@act_table,\@colm);



Code
    my ($arrayRef1 , $arrayRef2) = @_; 
my @actcol = @{$arrayRef1};
my @col = @{$arrayRef2};


 
 


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

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