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!

Recording only fist component of each kit instead of each component

Status
Not open for further replies.

jonathan314

IS-IT--Management
Jul 28, 2008
8
I got a program which should record each component of every kit in a DB and put it into a hash and then export into a file. For some reason it catch only the first component of its kit and then repeated for n times depending how many components its has.
The data looks like followings:

Kit Component LineKey Qty
221D0105B120K GW 000001 1
221D0105B120K S120OS7X5-AA 000002 0.5
221D0105B120K CR40B 000003 3.1
221D0105B120K HP40B 000004 2
221D0105B120K GG-A 000005 1
221D0105B120K NRG4OX-B 000006 1
221D0105B120K GBP 000007 0.8
221D0105B173K GW 000001 1
221D0105B173K S173OS7X5-AA 000002 0.6
221D0105B173K PLAT-OX-R 000003 0.9
221D0105B173K LABOR-RED 000004 4.5
221D0105B173K GNV 000005 6
221D0105B173K /C 000006 0

This is the code:
.........................................................
$SELECT = "SELECT IM_SalesKitDetail.SalesKitNo, \
IM_SalesKitDetail.ComponentItemCode, \
IM_SalesKitDetail.LineKey, \
IM_SalesKitDetail.QuantityPerAssembly \
FROM IM_SalesKitDetail";


if ($db->Sql($SELECT)) {
print "SQL failed.\n";
print "Error: " . $db->Error() . "\n";
} else {

while($db->FetchRow()) {

($kitid,$itemid,$lineKy,$qty) = $db->Data;
if (($kitid eq "") || ($itemid eq "")) {next;}
if ($FlagMap{$kitid} ne "KKK") {next;}
$cnt++;
$ItemMap{$kitid} .= $itemid . ',';
$QtyMap{$kitid} .= $qty . ',';
$LineKeyMap{$kitid} .= $lineKy . ',';
} #the while loop to walk through the data
print "Number of kit items: $cnt\n";

# Reorder the component for each kit
foreach $pk (sort keys %ItemMap) {
$items = $ItemMap{$pk};
@itemList = split ',', $items;

$qtys = $QtyMap{$pk};
@qtyList = split ',', $qtys;

$nLinekey = $LineKeyMap{$pk};
@nLinekeyList = split ',', $nLinekey;

$n = scalar(@itemList);
$cnt = 1;
while ($cnt <= $n) {
$found = 0;
for ($i = 0; $i <= $n; $i++) {

$KitMap{$pk} .= $itemList[$i] . "," . $qtyList[$i] . ",";
$cnt++;
$found = 1;
last;

}
if ($found == 0) {last;}
}
}
.......................................................
Please help to solve this issue.
Any suggestion is greatly appreciated.
 
Just a guess, try @qtyList = split(/,/, $qtys); (use //'s instead of '' in your split)
 
This seems to be an awful lot of messing about for something that should probably be quite trivial. How about letting the database do some of the work?
SQL:
my $SQL = "SELECT SalesKitNo, ComponentItemCode, LineKey, QuantityPerAssembly \
   FROM IM_SalesKitDetail \
   WHERE SalesKitNo <> '' AND ComponentItemCode <> '' \
   ORDER BY SalesKitNo, ComponentItemCode;"
If the columns are NULL rather than blank, you will need to reword it. If the contents of FlagMap come from another table, then use a JOIN to filter it further.

What's with the whole construct-your-own array with commas thing? Can't you just use a hash of arrays? Perhaps if you posted what the output should look like, we could shorten this quite a bit...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top