Gossamer Forum
Home : General : Perl Programming :

return two arrays from a sub

Quote Reply
return two arrays from a sub
is this legal?
Code:
...
my (@par,@whandicap) = &get_par_for_course;
...

sub get_par_for_course {
# --------------------------------------------------------
# lookup course and get par for each hole


&switch_to_courses;

my (%course) = &get_record2('1'); # always berkeley hills = #1


my (@par) = split (/:/, $course{'WPar'});
my (@whandicap) = split (/:/, $course{'WHandicap'});

return(@par,@whandicap);


}



or do i need separate routines to retrieve the two elements?
Quote Reply
Re: [delicia] return two arrays from a sub In reply to
ok figured a way to do it:
...
my ($wpar,$whandicap) = &get_par_handicaps;
my (@par) = split (/:/, $wpar);
my (@whandicap) = split (/:/, $whandicap);
...



sub get_par_handicaps {
# --------------------------------------------------------
# lookup course and get par for each hole

&switch_to_courses;

my (%course) = &get_record2('1'); # always berkeley hills = #1
return($course{'WPar'},$course{'WHandicap'});

}

Quote Reply
Re: [delicia] return two arrays from a sub In reply to
You can do it by making them arrayrefs:

Code:
my ($par,$whandicap) = &get_par_for_course;
foreach (@$par) {
....
}

foreach (@$whandicap) {
....
}
...or you can reference them:

Code:
my ($par,$whandicap) = (@$par,@$whandicap)



Code:
sub get_par_for_course {
# --------------------------------------------------------
# lookup course and get par for each hole


&switch_to_courses;

my (%course) = &get_record2('1'); # always berkeley hills = #1


my (@par) = split (/:/, $course{'WPar'});
my (@whandicap) = split (/:/, $course{'WHandicap'});

return(\@par,\@whandicap);


}

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!