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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with subroutine

Status
Not open for further replies.

ssmith001

Programmer
Oct 6, 2005
40
US
I am working on my first Perl script and I have a sub that calls a sub. The 1st sub returns a list of location/item/abccodes. If the abccode = 'A' then I want to call the 2nd sub to get and return the quantity. When I comment out the call to the 2nd sub, the 1st sub spins through and prints out all the correct data. When I uncomment and make the call to the second, as soon as it hits the first "A" qty, the sub return and goes onto the next location even thought there may be other A items at that location. I believe my problem is in the looping and/or the return but I'm not certain. Can someone help me?

Code:
sub get_items_for_loc_abc_stkclass
{
	  my $loc		 		= shift;
		my $abc 			= shift;
		my $stkclass 	= shift;
    
		$sth = $dbh->prepare( "SELECT a.loc
		                            , a.item
     														, a.p_abc
														 FROM stsc.sku a
   															, stsc.loc b
   															, stsc.item c
														WHERE a.loc = '$loc'
  														AND a.loc = b.loc
  														AND a.p_abc IN ($abc)
  														AND c.p_stkclass IN ($stkclass)
  														AND a.item = c.item 
  											 ORDER BY a.loc, a.item" );
	
		$sth->execute();        

		### Retrieve the returned rows of data
		my @row;
		while ( @row = $sth->fetchrow_array( ) )
		{
			if ( $row[2] eq 'A')
			{
    		LogMsg("\t  Item = " . $row[1] . "    ABC Code = " . $row[2] . "      A Qty = ");

			[COLOR=red]#my $A_qty = get_qty_for_A_items_cat3( $row[0], $row[1] );
    		#LogMsg("\t  Item = " . $row[1] . "    ABC Code = " . $row[2] . "      A Qty = " . $A_qty);[/color red]

			}
			else
			{
				LogMsg("\t  Item = " . $row[1] . "    ABC Code = " . $row[2] . "    BCE Qty = ");
			}		
		}
		warn "Data fetching terminated early by error: $DBI::errstr\n"
		if $DBI::err;
		#LogMsg("Done Fetching Rows");
}

sub get_qty_for_A_items_cat3
{
	  my $loc		 		= shift;
		my $item 			= shift;
        
    $sth = $dbh->prepare( "SELECT ROUND((CEIL((SUM (ROUND (a.totfcst)) - (b.oh + NVL(c.qty,0))) /  d.p_pdc_spq) * d.p_pdc_spq) * b.p_dlrnet)
    												 FROM stsc.dfutoskufcst a
	   														, stsc.sku b
	   														, stsc.schedrcpts c
	   														, stsc.item d
   													WHERE a.skuloc 		= '$loc'
     												  AND a.item   		= '$item'
     													AND a.startdate = ADD_MONTHS (TRUNC (SYSDATE, 'MM'), 1 ) 
     													AND a.item 			= b.item
	 														AND a.skuloc 		= b.loc
	 														AND b.item 			= c.item(+)
	 														AND b.loc 			= c.loc(+)
	 														AND a.item 			= d.item					   
												 GROUP BY b.oh
	   														, c.qty
	   														, d.p_pdc_spq
	   														, b.p_dlrnet" );
	
		$sth->execute();        
    
		### Retrieve the returned row of data
		my $row;
		while ($row = $sth->fetchrow_array( ) ) 
		{
			return ($row);
		}
		warn "Data fetching terminated early by error: $DBI::errstr\n"
		if $DBI::err;
}
 
Looks to me like it maybe a scoping issue. Where do you have $sth defined. Looks like the second sub uses the same $sth. That could be a problem. Either local the variable or have two statement handles defined ($sth_A, $sth_B).
 
Bingo, you nailed that one. I'm still trying to get my hands around this scoping thing. Now I'm getting a lot of these "Use of uninitialized value in concatenation (.) or string at promo.pl line 476." scattered around my output. Ideas?
 
These are the warnings in the code.( Received when use warnings is used.)
What is line no. 476?


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
This will produce an warning like the one you see:

my $a = undef;
print "$a is a nice variable\n";

Try it, you'll see.

So to correct this do the following or something like it:

print "$a is a nice variable\n" if $a;

...or...

if($a){
print "$a is a nice variable\n";
} else {
print "\$a is not nice. It is undefined!\n";
}
 
This is the line #476

Code:
LogMsg("Item = " . $itm . "    ABC Code = " . $abccode . "    A Qty = " . $A_qty);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top