The following script seems to work, but looks slow to me.
The purpose is to look at an array (recs) of strings with fields separated by ‘|’ characters.
By looking at certain fields (1 and 2) to form a key in uppercase,
I want to see if that is a duplicate key in the new ‘Order’ array.
I either save the key and position ‘p’ in a new element of ‘Order’
or I update it as a pointer to the most recent entry ‘recs’ if a duplicate is found.
Finally I sort the new ‘Order’ array and later parse the pointers (p) out so I can display the most recent ‘recs’ entry.
[code=php]
var Order = new Array(); // create artificial hash of KEYS|pointer
var str, i, j, p, fnd;
for (i=0; i<recs.length; i++) { // go thru database one time
p = i; // save DBase position
tmp = recs[i].split(‘|’); // get fields of each DBase record
// check for duplicate or revised entry.
str = (tmp[1]+tmp[2]).toUpperCase(); // form KEY of certain fields
for (j=0; j<Order.length; j++) { // look at prior elements of ‘Order’ array
fnd = false;
if (Order[j].match(str) != null) { p = j; Order[j] = str+’|’+p; fnd = true; } // if match found, update pointer
}
if (fnd == false) { Order.push(str+’|’+p); } // if no match, then create “KEY|pointer”
}
Order.sort();
Is there a more efficient way to check for duplicate entries and update to the most recent entry in recs if found
without using the ‘match()’ or without going through the ‘Order’ array repeated times with increasing elements?
The code works … I’m just looking for a more efficient method as the ‘Order’ array becomes larger and larger.
Any ideas are appreciated as I’m willing to try other approaches if necessary.