...

View Full Version : count words in a table column



Basscyst
09-28-2003, 07:17 PM
I'm trying to write somthing (and failing) that will give me the most common entry in a table column. In other words, tell me the word that is in this column the most times and tell me how many times it is there. Is this easily done? Can't seem to get my head around it.

Thanks for any efforts,

Basscyst

cheesebag
09-29-2003, 12:15 AM
See if these help:

http://www.mozilla.org/docs/dom/technote/tn-dom-table/

http://www.webreference.com/js/column40/index.html

Willy Duitt
09-29-2003, 01:20 AM
You may find this Word Analyzer Script (http://www-level3.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20720492.html) helpful.

Basscyst
09-29-2003, 01:23 AM
That helps a bit. I've actually been able to get all of the desired information into an array ie:

var userId=new Array();
userId[0]=("users name1","0");
userId[1]=("users name2","0");

etc. . .

and I can get it to add 1 to userid[x][1] each time the entry is listed. But now I don't know how to pull out just the record with the highest value in userid[x][1].

Any further thoughts?

Basscyst
09-29-2003, 01:30 AM
Not quite what I'm looking for Willy, but mabeye with some minor modifications. Thanks.

cheesebag
09-30-2003, 01:13 AM
This assumes a table with uniform columns; the displayed data strings also need to be unbroken by HTML tags (although they can be entirely nested in them). That said:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>untitled</title>
<style type="text/css">

body {
margin: 20px;
background: steelblue;
}

table {
margin: 30px auto;
border: 4px #aaccff dashed;
border-collapse: collapse;
}

th {
font: 200 11px tahoma;
border: 1px black dashed;
background: buttonface;
}

td {
width: 80px;
height: 20px;
font: 200 10px tahoma;
text-align: center;
border: 1px silver dashed;
background: #f8f8ff;
}

#readout {
width: 640px;
font: 200 13px tahoma;
color: ghostwhite;
margin: auto;
}

</style>
<script type="text/javascript" language="javascript">

function tallyColumns(table_id)
{
var _table, _tbody, _tr, _node, _text, rows, num_rows, cols, num_cols, r, c, n;
var columns = [];
_table = document.getElementById(table_id);
_tbody = _table.getElementsByTagName('tbody')[0];
rows = _tbody.getElementsByTagName('tr');
num_rows = rows.length;
for (r = 0; r < num_rows; ++r)
{
_tr = rows.item(r);
if (_tr.className == 'noTally')
continue;
cols = _tr.getElementsByTagName('td');
num_cols = cols.length;
for (c = 0; c < num_cols; ++c)
{
if (columns[c] == null)
columns[c] = [];
_node = cols.item(c);
while (_node.hasChildNodes())
_node = _node.lastChild;
if (_node.nodeType == 3 && _node.data != '')
_text = _node.data;
else continue;
if (!columns[c][_text])
{
columns[c][columns[c].length] = new Object();
columns[c][columns[c].length - 1].text = _text;
columns[c][columns[c].length - 1].freq = 1;
columns[c][_text] = columns[c][columns[c].length - 1];
}
else columns[c][_text].freq++;
}


}
for (c = 0; c < num_cols; ++c)
columns[c].sort(function(a,b) { return b.freq - a.freq });
return columns;
}

</script>
</head>
<body>
<table id="tally">
<tr>
<th>&amp;#149; column 1 &amp;#149;</th>
<th>&amp;#149; column 2 &amp;#149;</th>
<th>&amp;#149; column 3 &amp;#149;</th>
<th>&amp;#149; column 4 &amp;#149;</th>
<th>&amp;#149; column 5 &amp;#149;</th>
<th>&amp;#149; column 6 &amp;#149;</th>
<th>&amp;#149; column 7 &amp;#149;</th>
<th>&amp;#149; column 8 &amp;#149;</th>

<script type="text/javascript" language="javascript">

var values = [ 'Alpha' , 'Bravo' , 'Charlie' , 'Delta' , 'Echo' , 'Foxtrot' , 'Golf' , 'Hotel' ];
var r, c;
for (r = 0; r < 12; ++r)
{
document.writeln('</tr><tr>');
for (c = 0; c < 8; ++c)
document.writeln('<td>' + values[Math.floor(Math.random() * values.length)] + '</td>');
}

</script>
</tr>
<tr class="noTally">
<td colspan="8">-- dummy row --</td>
</tr>
</table>
<div id="readout">
<script type="text/javascript" language="javascript">

var columns = tallyColumns('tally');
var col, el, i = 0, j, str = '';
while (col = columns[i++])
{
j = 0;
str += '<br /><b>Column ' + i + ':&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</b>';
while (el = col[j++])
str += '&amp;nbsp;&amp;nbsp;' + el.text + ' [<b>' + el.freq + '</b>]&amp;nbsp;&amp;nbsp;';
}
document.writeln(str);

</script>
</div>
</body>
</html>

tallyColumns() returns an array of table columns; each element is an object with .text & .freq properties reflecting the number of occurrances (the latter) of the string (the former). All the element (arrays) are sorted. You can tag rows not to be included in this process with class="noTally". I'll see if I can clean this up a bit when I have a chance.

cheers, cb



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum