Divide By Zero

Divide By Zero
I have some perl/sql code where I want the 3rd column a percent which will be the result of 2rd column divided by the 1st column.

I think someting like
my $percentage = ($row[1] ne ‘0′) ? ($row[2] / $row[1]) : 0;
needs to be put in the code or something like this.
So if row1 is 0 then put a “0%”,otherwise do the cal.

sample table of my problem

Col_0 Col_1 Col_2 Col_3
HCI-D 0 1 #DIV/0!
HCI-O 5 2 0.4

Actually code that I need changed.

$sth = $dbh->prepare( “SELECT
HCI_IND,
Sum(ASSIGN_CNT_1) AS SumOfCNT,
Count(HCI_IND) AS CountOfHCI
FROM PIVOT.CIRCUIT_INST_ID = PROFILE1.NTWRK_CIRC_DTL.CIRCUIT_INST_ID
GROUP BY HCI_IND

;” );

$sth->execute();

$dbh2 = DBI->connect( “dbi:Pg:dbname=iwmirror”, “postgres”, “football” ) ||
die( “Can’t connect to database.\n” );

$sth2 = $dbh2->prepare(“Delete from hci_sum”);
$sth2->execute();
while( at_sybmol_here row = $sth->fetchrow() ) {

$sth3 = $dbh2->prepare( “INSERT INTO hci_sum VALUES (
‘$row[0]’, ‘$row[1]’,’$row[2]’)” );
$sth3->execute();
}

open( RING, “>AAA.txt” ) || die( “Can’t open file for writing.\n” );

print RING join( ‘%%’, at_sybmol_here row ), “\n”;

close( RING );
$sth->finish();
$dbh->disconnect();
$dbh2->disconnect();

#$time_end = time(); #timestamp in number of seconds since 01-Jan-1970

#print TIMING “$time_end: Finishing sonet_rings.pl. ” .
# ( $time_end – $time ) / 60 . “mins. \n”;
#close( TIMING );

Leave a Reply

Your email address will not be published. Required fields are marked *