I have further modified the script below from some referenced code I found after some help from ‘astupidname’.
Earlier resolved post was: [url]http://www.webdeveloper.com/forum/showthread.php?t=218910
This current script is just a protion of a test program to make sure all functions perform as expected.
All works well except for one small area associated with the HashTable.prototype.modify().
If you run the following script without the checkbox checked, all works fine.
With the checkbox checked, an additional statement is executed to modify the ‘Gasoline Alley’ entry to change the date display.
It does modify the record correctly with either version of the modify() function,
EXCEPT, the display after the last record shows an ‘undefined’ that is NOT EXPECTED.
Any reason for the ‘undefined’ display after the ‘Daffy Duck’ record display when the earlier record is modified?
?
Obviously, something is different, but I’ll be darned if I can see it.
I’ve highlighted the areas that I suspect are causing the problem,
but I don’t see what to change (assuming I’m looking in the right area).
Does anyone have an explaination or just a better set of eyes to see the problem?
Here’s the test code I’m working with:
[code]
<html>
<head>
<title>HashTable Sample</title>
<script type=”text/javascript”>
// From: http://erik.eae.net/archives/2005/06/06/22.13.54/
function HashTable() { this._hash = {}; }
HashTable.prototype.add = function (key, val) { this._hash[key] = val; };
[COLOR=”Red”]<!– possible problem function –>[/COLOR]
HashTable.prototype.modify = function (key, val) { // problem function ???
this.add(key,val); // alternative to line below
// this.remove(key); this.add(key,val);
} // either 2 lines above act as a value modify statement
HashTable.prototype.append = function (key, val, sep) {
var value = this.getItem(key);
this._hash[key] = value+sep+val;
} // add to current value with ‘sep’ char
HashTable.prototype.remove = function (key) { delete this._hash[key]; };
HashTable.prototype.getItem = function (key) {
if (this._hash.hasOwnProperty(key)) { return this._hash[key]; }
return undefined;
}
HashTable.prototype.containsKey = function (key) { return this._hash.hasOwnProperty(key); };
// —————————————————- start of special needs functions
HashTable.prototype.containsValue = function (val) {
var res = [];
var keys = this.getKeys();
var vals = [];
for (k=0; k<keys.length; k++) {
vals = this.getItem(keys[k]).split(‘|’); // uses ‘|’ as seperator for multiple values
// vals.pop();
if (vals.in_array(val)) { res.push(keys[k]+’:’+val); }
}
return res;
}
HashTable.prototype.countValue = function (val) {
var res = [];
var cnt = 0;
var keys = this.getKeys();
var vals = [];
for (k=0; k<keys.length; k++) {
vals = this.getItem(keys[k]).split(‘|’); // uses ‘|’ as seperator for multiple values
// vals.pop();
cnt = vals.in_array_Count(val);
if (cnt > 0) { res.push(keys[k]+’:’+val+’:’+cnt); }
}
return res;
}
HashTable.prototype.getKeyValues = function() {
var res = [];
var keys = this.getKeys();
// keys = keys.sort(); // optional – display in sorted order
var vals = [];
for (k=0; k<keys.length; k++) {
vals = this.getItem(keys[k]); // uses ‘|’ as seperator for multiple values
res.push(keys[k]+’:’+vals);
}
return res;
}
HashTable.prototype.getKeys = function () {
var res = [];
for (var k in this._hash) { if (this._hash.hasOwnProperty(k)) { res.push(k); } }
return res;
};
HashTable.prototype.getValues = function () {
var res = [];
for (var k in this._hash) { if (this._hash.hasOwnProperty(k)) { res.push(this._hash[k]); } }
return res;
}
// search array for value: returns true or false;
Array.prototype.in_array = function(p_val) {
for (var i = 0, l = this.length; i < l; i++) {
if (this[i] == p_val) { return true; } // checks for absolute match (not appended values)
}
return false;
}
// search array for value: return count of occurances (0=none, n=…)
Array.prototype.in_array_Count = function(p_val) {
var cntr = 0;
for (var i = 0, l = this.length; i < l; i++) { // checks for absolute match (not appended values)
if(this[i] == p_val) { cntr++; }
}
return cntr;
}
// —————————————————- end of special needs code
</script>
<script type=”text/javascript”>
function testHashTable() {
var str = ”;
myHash = new HashTable();
myHash.add(‘Gasoline Alley’,’08/16/1947′);
myHash.add(‘Betty Boop’,’07/30/1951′);
myHash.add(‘Cache Cow’,’05/29/1970′);
myHash.add(‘Daffy Duck’,’08/03/1971′);
str += ‘nFOLLOWING EFFECTED BY MODIFY RECORD CHECKBOX CHANGE’;
// ==================================================
// unexplained ‘undefined’ in following display ???
str += ‘nnAll Key:Value pairsn’+myHash.getKeyValues().join(‘n’);
// caused by the insertion of either of the following lines (not both)
// seems to effect the output of the previous line when following line(s) are used
/* [COLOR=”Red”]only difference in the display[/COLOR] */
if (document.getElementById(‘cbModify’).checked == true) {
// str += myHash.add(‘Gasoline Alley’,’09/16/1977′); // modify item value
str += myHash.modify(‘Gasoline Alley’,’09/16/1977′); // modify item value (same as add?)
}
str += ‘nnEND OF AREA EFFECTED BY MODIFY RECORD CHECKBOX CHANGE’;
// ==================================================
str += ‘nnBack to normal expected display’;
str += ‘nAll Key:Value pairsn’+myHash.getKeyValues().join(‘n’);
document.getElementById(‘TArea’).value = str;
}
</script>
</head>
<body>
<button onclick=”testHashTable()”>Test HashTable</button>
<input type=”checkbox” id=”cbModify”> Modify record <br>
<textarea id=”TArea” rows=”30″ cols=”60″></textarea>
</body>
</html>