Help - Search - Members - Calendar
Full Version: Accumulation items in a table
HTMLHelp Forums > Programming > Client-side Scripting
denmarks
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
Christian J
QUOTE(denmarks @ May 28 2012, 06:15 PM) *
That field

Do you mean a (form) field or a table cell?

Also I'm not sure I understand the purpose: does the site visitor type letters in a form field? (If OTOH you create the content yourself I don't see the point of a validation script. unsure.gif )

QUOTE
can contain any letters of the alphabet.

Do you mean a particular alphabet's charset? If any language's alphabet is allowed things may get hairy, if nothing else since you need a way for the script to separate various alphabets' characters from special characters).

QUOTE
I also need the best way to scan the table and the individual cell that would be compatible with multiple browsers.

Support in today's browsers shouldn't be a problem.
denmarks
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.
Brian Chandler
QUOTE
This a table that I create but I constantly change.


"Change"? How, by some server script? If so then obviously the script should count the totals, which is easy. Using javascript is usually making things more difficult for yourself.
Christian J
QUOTE(denmarks @ May 29 2012, 01:37 AM) *

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
QUOTE(Christian J @ May 29 2012, 11:14 AM) *

QUOTE(denmarks @ May 29 2012, 01:37 AM) *

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.
Christian J
QUOTE(denmarks @ May 29 2012, 08:52 PM) *

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
QUOTE(Christian J @ May 29 2012, 03:40 PM) *

QUOTE(denmarks @ May 29 2012, 08:52 PM) *

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
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.