Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Subroutine Question Involving Arrays...

Status
Not open for further replies.

StormMedic85

Programmer
Sep 7, 2007
20
US
I can't seem to figure out why the subroutine is only returning the 3 values for the last element in the array I am passing to it in the foreach statement. The array @juliandate contains two elements, so I'd like to perform the calculation for both elements. Can anyone enlighten me as to where I'm going wrong? I am quite a beginner with Perl so try not to laugh too hard at my stupid mistakes and bad formatting. ;0) Thanks for your help!


sub Jul2Greg {

use integer;

my ($a, $g, $dg, $c, $dc, $b, $db, $da, $y);
my ($m, $d, $sgregyear, $sgregmonth, $sgregday, $i);

foreach $i(@juliandate) {

#Other parts of algorithm omitted.
$sgregyear = $y - 4800 + ($m + 2)/12;
$sgregmonth = (($m + 2)%12)+1;
$sgregday = $d + 1;

}

return($sgregmonth, $sgregday, $sgregyear);

}
 
Hi,
you don't modify @juliandate, and you don't return @juliandate. You only return 3 scalar values. So what do you expect?
Anyway, making an array a global variable is in general not a good idea. Try to pass the array as an array reference to your sub, then modify it, and you have to return nothing. Or, if you want to leave your original array unmodified, pass it as an array, create a new array, do all the calculations on that one end return it as an array or array reference, that's up to you. I prefer references.
HTH,
yai
yai
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top