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:
Problem with function returning several arraies

 



ningji
Novice

Jun 12, 2013, 3:09 PM

Post #1 of 6 (343 views)
Problem with function returning several arraies Can't Post

Hi i have a function very similar to this

do_work
{
my (@arr1, @arr2);

......
push (@arr1, "a");
push (@arr1, "b");
push (@arr2, "1");
push (@arr2, "2");
return (@arr1, @arr2);
}

my (@Arr1, @Arr2) = do_work();

for($i=0; $i<=$#Arr1; $i++)
{
print $Arr1[$i]." ".$Arr2[$i]."\n";
}

but the result is not correct, what did i miss please ?


BillKSmith
Veteran

Jun 12, 2013, 3:57 PM

Post #2 of 6 (337 views)
Re: [ningji] Problem with function returning several arraies [In reply to] Can't Post

Your arrays are "flattened" into a single list. Return a list of array references.

Code
use strict; 
use warnings;
sub do_work {
my ( @arr1, @arr2 );

push( @arr1, "a" );
push( @arr1, "b" );
push( @arr2, "1" );
push( @arr2, "2" );
return ( \@arr1, \@arr2 );
}

my ( $Arr1, $Arr2 ) = do_work();

for ( my $i = 0; $i <= $#$Arr1; $i++ ) {
print $$Arr1[$i], ' ', $$Arr2[$i], "\n";
}

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Jun 13, 2013, 4:11 PM

Post #3 of 6 (320 views)
Re: [BillKSmith] Problem with function returning several arraies [In reply to] Can't Post

Or perhaps this way is slightly simpler (with the subroutine returning arrayrefs supplied by Bill):


Code
my ( @Arr1, @Arr2 ); 
( \@Arr1, \@Arr2 ) = do_work();

for my $i (0..$#Arr1) {
print $Arr1[$i], ' ', $Arr2[$i], "\n";
}



FishMonger
Veteran / Moderator

Jun 13, 2013, 6:49 PM

Post #4 of 6 (314 views)
Re: [Laurent_R] Problem with function returning several arraies [In reply to] Can't Post


In Reply To
Or perhaps this way is slightly simpler (with the subroutine returning arrayrefs supplied by Bill):


Code
my ( @Arr1, @Arr2 ); 
( \@Arr1, \@Arr2 ) = do_work();

for my $i (0..$#Arr1) {
print $Arr1[$i], ' ', $Arr2[$i], "\n";
}



That generates a compilation error.


Quote
D:\test>Laurent.pl
Can't modify reference constructor in list assignment at D:\test\Laurent.pl line 14, near ");"
Execution of D:\test\Laurent.pl aborted due to compilation errors.



Laurent_R
Veteran / Moderator

Jun 14, 2013, 11:18 AM

Post #5 of 6 (305 views)
Re: [FishMonger] Problem with function returning several arraies [In reply to] Can't Post

Alright, I thought it would work this way, but I could not test anything as I am on a business trip with no Perl environment. So, my error, sorry, but it is quite easy to fix it by doing the things the other way arouind.


ningji
Novice

Jun 21, 2013, 2:46 PM

Post #6 of 6 (295 views)
Re: [BillKSmith] Problem with function returning several arraies [In reply to] Can't Post

it worked, thanks everyone !


In Reply To
Your arrays are "flattened" into a single list. Return a list of array references.

Code
use strict; 
use warnings;
sub do_work {
my ( @arr1, @arr2 );

push( @arr1, "a" );
push( @arr1, "b" );
push( @arr2, "1" );
push( @arr2, "2" );
return ( \@arr1, \@arr2 );
}

my ( $Arr1, $Arr2 ) = do_work();

for ( my $i = 0; $i <= $#$Arr1; $i++ ) {
print $$Arr1[$i], ' ', $$Arr2[$i], "\n";
}



 
 


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

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