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!

Using a value if it is found in an array 1

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I am reading one table and creating an array that will be used many times.

The second Select returns values, if the pay-sum-grp value is found in the array I need to add the wage-amount to the variable $vGrossRG. I loop through all of the entries in this table for any match to a value in the array RG2.

The grep statement

if (grep {$_ eq $Element} @RG2) {
my $vGrossRG = ($vGrossRG + $dat9e[1]);
}
}
appears to not be working correctly.

I know I am not the expert that many of you are. Can somebody please tell me what I am am doing wrong?

Perl:
## read table LAWSON.EMPLOYEE and process each entry
use DBI;

$dbh=DBI->connect('dbi:ODBC:LawDEV', $user, $pass,
		{RaiseError => 1, odbc_cursortype => 2}) or die;

## build the current array of RG2 pay-sum-grp values
    $st9a = $dbh->prepare("SELECT PAY_SUM_GRP FROM PSGRELATE WHERE
	  		COMPANY   = '" . $vComp . "' and
			PAY_CLASS = 'RG2'") or die "select 9a: " . $db9a->errstr;

    @dat9a;
	@RG2;

    $r9a = $st9a->execute or die "Couldn't exe 9a: " . $st9a->errstr;

    while (@dat9a = $st9a->fetchrow_array()) {

        push(@RG2, $dat9a[0]);
		}

 	$st9a->finish;

	$string1 = join(", ",@RG2);

##	print $string1 . "\n";	

        	$st9e = $dbh->prepare("SELECT PAY_SUM_GRP, WAGE_AMOUNT 
				FROM PRTIME WHERE
		  		COMPANY    = '" . $vComp . "' and
		  		EMPLOYEE   = '" . $vEmp . "' and
          		CHECK_ID   = '" . $vChkId . "'") 
				or die "select 9e: " . $db9e->errstr;

        	my @dat9e;
			my $Element = $dat9e[0];
			my $vGrossRG = 0;

        	my $r9e = $st9e->execute or die "Couldn't exe 9e: " . $st9e->errstr;

        	while (@dat9e = $st9e->fetchrow_array()) {
       	        if (grep {$_ eq $Element} @RG2) {
        			my $vGrossRG = ($vGrossRG + $dat9e[1]);
					}
				}

 		    $st9e->finish;

			$Tag01 = "<GrossBaseEarnings>";
			$Tag02 = "<\/GrossBaseEarnings>";

            $sLine = $Tag01 . $vGrossRG . $Tag02 . "\n";
            print $sLine;
 
SQL is powerful, why not use it? Mine is rusty, but something like this should chop out most of that code:

SQL:
SELECT SUM(T.WAGE_AMOUNT)
FROM PSGRELATE R, PRTIME T
WHERE R.PAY_SUM_GRP = T.PAY_SUM_GRP
AND R.PAY_CLASS = 'RG2'
AND R.COMPANY = '$vComp'
AND T.COMPANY = '$vComp'
AND T.EMPLOYEE = '$vEmp'
AND T.CHECK_ID = '$vChkId'

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top