Accumulation items in a table |
Accumulation items in a table |
denmarks |
May 28 2012, 11:15 AM
Post
#1
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
I have a table where one of the columns is activities. That field can contain any letters of the alphabet. There can be no duplicates within a cell but there can be none or any. There is a blank between each letter for readability. For example:
A C D F G A B [blank] M N O R Z What is the best way to scan the table and count how many of each letter appears? I assume creating an array and indexing by the letter may work best for the variables. I also need the best way to scan the table and the individual cell that would be compatible with multiple browsers. Result A - 2 B - 1 C - 1 etc |
denmarks |
May 28 2012, 06:37 PM
Post
#2
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
This a table that I create but I constantly change. I wanted an automated way to count the letters.
This is for a group going on a cruise. A - means they will attend event XXX B - means they will attend event YYY C - means they will attend event ZZZ See http://hawaii.dmmarks.com I want an automated way to count the number of people attending each event. Right now I manually count them and since changes are made often I sometimes make a mistake. I did find one method but it worked in Firefox and not in IE. Also the counting of the letters in a cell was too slow. |
Christian J |
May 29 2012, 01:14 PM
Post
#3
|
. Group: WDG Moderators Posts: 9,665 Joined: 10-August 06 Member No.: 7 |
I want an automated way to count the number of people attending each event. Right now I manually count them and since changes are made often I sometimes make a mistake. It's relatively easy to find an occurence of say letter A in a cell, and then add up all such occurences. Duplicates can then be excluded from the count (that is, multiple A letters in a cell still just count as one). Do you also want to remove duplicates in each cell for "esthetic" reasons? That's more tricky (=bug prone), especially if there are linebreaks etc in the code. |
denmarks |
May 29 2012, 01:52 PM
Post
#4
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
I want an automated way to count the number of people attending each event. Right now I manually count them and since changes are made often I sometimes make a mistake. It's relatively easy to find an occurence of say letter A in a cell, and then add up all such occurences. Duplicates can then be excluded from the count (that is, multiple A letters in a cell still just count as one). Do you also want to remove duplicates in each cell for "esthetic" reasons? That's more tricky (=bug prone), especially if there are linebreaks etc in the code. There will never be duplicates since I an entering the letters. There are no forced line breaks. There is a space between each letter. I need to do 2 things. I need a way to do a loop through the cells that will work for any browser. The contents of a cell will then be assigned to a variable. This is where I had a problem of it working for Firefox and not IE. I have found routines that will accumulate numbers in the cells and I tried to alter one of them. The problem occurred when trying to place the contents of the cell into a variable. It worked in Firefox but was null in IE. Sorry I did not keep the code. Now I need the best way to accumulate the count of each letter. I do not want to create 26 if statements. There must be a more esoteric way to do a scan of the variable and for each letter add one to its corresponding accumulator. I believe that arrays can be indexed by a name (letter) rather than occurrence number. My first try was with 26 if statements and 26 variables. This is too much code. There must be an easier way. Note that I do this for fun and as a learning experience, not because it must be done. Obviously I can continue to do the count manually. I only want to program it if it can be done with a relatively short routine. Once I have the accumulation I have no problem putting it on the page. I do it with date counts now. This post has been edited by denmarks: May 29 2012, 01:53 PM |
Christian J |
May 29 2012, 05:40 PM
Post
#5
|
. Group: WDG Moderators Posts: 9,665 Joined: 10-August 06 Member No.: 7 |
There will never be duplicates since I an entering the letters. There are no forced line breaks. There is a space between each letter. Then I misunderstood everything, I thought you wanted a kind of validation script to catch typos. Counting correct cell content is much easier. QUOTE I believe that arrays can be indexed by a name (letter) rather than occurrence number. That's called an associative array. Here's an example that seems to work in my browsers: CODE <table border=1 id="memberTable"> <tr> <th>foo</th> <th>bar</th> </tr> <tr> <td>text</td> <td>A B C</td> </tr> <tr> <td>text</td> <td>B A</td> </tr> <tr> <td>text</td> <td>B</td> </tr> <tr> <td>text</td> <td></td> </tr> </table> <p id="totals"></p> <script type="text/javascript"> var cruise_events={'A':0, 'B':0, 'C':0}; // default values are zero for each cruise event var table=document.getElementById('memberTable'); var totals=document.getElementById('totals'); for(var letter_item in cruise_events) { var num=0; // tally for(var i=1; i<table.rows.length; i++) { // get cell content of last cell in row var row_cells=table.rows[i].cells; var last_cell=row_cells[row_cells.length-1]; var cell_content=last_cell.innerHTML; // tally of each cruise event if(cell_content.indexOf(letter_item)!=-1) { num++; } } cruise_events[letter_item]=num; } // display results for(var letter_item in cruise_events) { totals.innerHTML+=letter_item+': '+cruise_events[letter_item]+'<br>'; } </script> |
denmarks |
May 29 2012, 06:07 PM
Post
#6
|
Advanced Member Group: Members Posts: 207 Joined: 17-January 08 Member No.: 4,734 |
There will never be duplicates since I an entering the letters. There are no forced line breaks. There is a space between each letter. Then I misunderstood everything, I thought you wanted a kind of validation script to catch typos. Counting correct cell content is much easier. QUOTE I believe that arrays can be indexed by a name (letter) rather than occurrence number. That's called an associative array. Here's an example that seems to work in my browsers: CODE <table border=1 id="memberTable"> <tr> <th>foo</th> <th>bar</th> </tr> <tr> <td>text</td> <td>A B C</td> </tr> <tr> <td>text</td> <td>B A</td> </tr> <tr> <td>text</td> <td>B</td> </tr> <tr> <td>text</td> <td></td> </tr> </table> <p id="totals"></p> <script type="text/javascript"> var cruise_events={'A':0, 'B':0, 'C':0}; // default values are zero for each cruise event var table=document.getElementById('memberTable'); var totals=document.getElementById('totals'); for(var letter_item in cruise_events) { var num=0; // tally for(var i=1; i<table.rows.length; i++) { // get cell content of last cell in row var row_cells=table.rows[i].cells; var last_cell=row_cells[row_cells.length-1]; var cell_content=last_cell.innerHTML; // tally of each cruise event if(cell_content.indexOf(letter_item)!=-1) { num++; } } cruise_events[letter_item]=num; } // display results for(var letter_item in cruise_events) { totals.innerHTML+=letter_item+': '+cruise_events[letter_item]+'<br>'; } </script> THANKS |
Lo-Fi Version | Time is now: 8th May 2024 - 05:44 PM |