--- On Tue, 2/1/11, Uri Guttman <u...@stemsystems.com> wrote:
> From: Uri Guttman <u...@stemsystems.com> > Subject: Re: problem getting multiple values returned from a subroutine > To: "loan tran" <loan...@yahoo.com> > Cc: beginners@perl.org > Date: Tuesday, February 1, 2011, 10:59 AM > >>>>> "lt" == loan > tran <loan...@yahoo.com> > writes: > > lt> I'm having problem getting multiple values > returned from a subroutine. > > lt> > ############################################################################ > lt> # Get required subroutines which need to be > included. > # > lt> > ############################################################################ > lt> require > "/home/sybase/scripts/pl/prod/global_dcapdsg001.pl"; > lt> require > "/home/sybase/scripts/pl/prod/dataStage_dcapdsg001.pl"; > > don't use require and .pl files. use 'use' and .pm files. > that allows > for importing subs which is easier to manage with multiple > namespaces > > lt> my ($company_header, $company_name, $period, > $GLYear) = parse_title(); > > lt> > ############################################################################# > lt> # SUB ROUTINE > lt> > ############################################################################# > > do you really need that block comment? > > lt> sub parse_title{ > lt> # Determine what company, month, > and year to load by parsing the report title > > lt> open IN, '<', $po_file or die > "cannot open $po_file because $!\n"; > > you are using a global there. pass the file name as an > argument. much > cleaner > > lt> my $found_C > = 0; > lt> my $found_P > = 0; > > lt> while (my $line = <IN>) { > lt> chomp; > > that is chomping $_. you read into $line which isn't > chomped > > lt> if ($line =~ > /^\*.*Company:.*\*$/){ > lt> #DataSample: * > Company: 205 EDEN MEDICAL > CENTER * > lt> print "Line: $line > \n"; > lt> my $company_header > = substr($line,32,3); > lt> my $company_name = > substr($line,38,35); > > using substr is a weak way to parse a line unless it really > is fixed > fields. better to use a regex to extract all the parts you > want. > > lt> print "Company: > $company_header\nCompany Name: $company_name\n"; > lt> $found_C= 1 ; > lt> } > lt> if ($line =~ > /^.*Inventory Items As Of Cutoff Period.*\d\d\d\d$/){ > lt> #Data Sample: > Inventory Items As Of Cutoff Period 12 December 31, > 2010 > lt> print "Line: > $line\n"; > lt> my $period = > substr($line,77,2); > lt> print "Period: > $period \n"; > lt> my $GLYear = > substr($line,-5,4); > lt> print "GLYear: > $GLYear \n"; > lt> $found_P = 1; > lt> } > lt> #If found both > company and period, no need to scan the rest of the file. > lt> last if ($found_C == > 1 and $found_P == 1); > > lt> }#end while > lt> close IN; > lt> #return($company_header, > $company_name, $period, $GLYear); > > and why is that commented out? it looks fine. > > lt> return("$company_header", > "$company_name", "$period", "$GLYear"); > > that won't be any different with regard to returning > multiple > values. but it is wrong to quote scalars when you don't > need to. > > lt> 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? > > do your regexes work? do you see those debug prints in your > if blocks? Yes they do. Below is the program output: Line: * Company: 361 SUTTER WEST BAY SERVICE CENTER * Company: 361 Company Name: SUTTER WEST BAY SERVICE CENTER Line: Inventory Items As Of Cutoff Period 12 December 31, 2010 Period: 12 GLYear: 2010 , , , comany or year or month does not look right > if not, you exit the loop without ever setting the vars so > you return > nothing useful. > > uri > > -- > Uri Guttman ------ u...@stemsystems.com > -------- http://www.sysarch.com -- > ----- Perl Code Review , Architecture, Development, > Training, Support ------ > --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/