On 01/02/2011 18:51, loan tran wrote:
I'm having problem getting multiple values returned from a subroutine. Thanks in advance for pointers. My codes:
<snip>
sub parse_title{ # Determine what company, month, and year to load by parsing the report title open IN, '<', $po_file or die "cannot open $po_file because $!\n"; my $found_C = 0; my $found_P = 0;
You also need to declare here my ($company_header, $company_name, $period, $GLYear); as at present the variables are restricted to the block where they are declared and calculated. By the time the return statement comes they are out of scope and their values are lost.
while (my $line =<IN>) { chomp; if ($line =~ /^\*.*Company:.*\*$/){ #DataSample: * Company: 205 EDEN MEDICAL CENTER * print "Line: $line \n"; my $company_header = substr($line,32,3); my $company_name = substr($line,38,35);
Just $company_header = substr($line,32,3); $company_name = substr($line,38,35); is all that's needed here.
print "Company: $company_header\nCompany Name: $company_name\n"; $found_C= 1 ; } if ($line =~ /^.*Inventory Items As Of Cutoff Period.*\d\d\d\d$/){ #Data Sample: Inventory Items As Of Cutoff Period 12 December 31, 2010 print "Line: $line\n"; my $period = substr($line,77,2); print "Period: $period \n"; my $GLYear = substr($line,-5,4); print "GLYear: $GLYear \n";
Likewise, this should be $period = substr($line,77,2); print "Period: $period \n"; $GLYear = substr($line,-5,4); print "GLYear: $GLYear \n";
$found_P = 1; } #If found both company and period, no need to scan the rest of the file. last if ($found_C == 1 and $found_P == 1); }#end while close IN; #return($company_header, $company_name, $period, $GLYear); return("$company_header", "$company_name", "$period", "$GLYear"); } #end sub __END__ The problem is : I expect to see: 361, WEST BAY SERVICE CENTER, 12, 2010 from the print statement above but only get , , , intead. What is wrong with my codes?
I hope this helps you. Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/