Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts

    count words in a table column

    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
    Last edited by Basscyst; 09-28-2003 at 07:30 PM.

  • #2
    Regular Coder
    Join Date
    Aug 2003
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
     ........ another wild guess ........

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You may find this Word Analyzer Script helpful.
    Last edited by Willy Duitt; 09-29-2003 at 01:24 AM.

  • #4
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    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?
    Last edited by Basscyst; 09-29-2003 at 01:46 AM.

  • #5
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Not quite what I'm looking for Willy, but mabeye with some minor modifications. Thanks.
    Last edited by Basscyst; 09-29-2003 at 01:45 AM.

  • #6
    Regular Coder
    Join Date
    Aug 2003
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    
    <!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
    Last edited by cheesebag; 09-30-2003 at 04:09 AM.
    &nbsp;........ another wild guess ........


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •