Gossamer Forum
Home : Products : DBMan : Customization :

Count the yes answers

Quote Reply
Count the yes answers
JPDeni was kind enough to help me with an average/image problem a few weeks back that is working wonderfully. You can view it at http://www.gossamer-threads.com/...orum.cgi?post=283017.

We have now made an addition to the database that has a lot of yes/no answers. What I would like to do is to be able to count the number of yes answers and return the percentage of the total entries in that field where the answer was "Yes". A sample result would be "67% Yes"

Any help would be appreciated.

Buddy
Quote Reply
Re: [Budb] Count the yes answers In reply to
In the files for your average computation, there's a subroutine called sub compute_averages. There's a section that looks like this:
Code:
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; } # Skip and Remove blank lines
if ($line =~ /^#/) { $output .= $line; next LINE; } # Comment Line
chomp ($line);
@data = &split_decode($line);
if ($data[0] > 0) {
++$number_of_records;
for ($i = 0; $i<=5; $i++) {
$total[$i] += $data[$i];
}
}
}

if ($number_of_records > 0) {
for ($i = 0; $i<=5; $i++) {
$average[$i] = $total[$i] / $number_of_records;
$average[$i] = int($average[$i] + .5);
}
}
$output = join '|', @average;

This assumes that fields 0 through 5 contain numbers you want to average. Let's say that fields 6 through 9 contain the "Yes/No" answers that you want to find the percentages of. Add the following bolded code:
Code:
LINE: foreach $line (@lines) {
if ($line =~ /^$/) { next LINE; } # Skip and Remove blank lines
if ($line =~ /^#/) { $output .= $line; next LINE; } # Comment Line
chomp ($line);
@data = &split_decode($line);
if ($data[0] > 0) {
++$number_of_records;
for ($i = 0; $i<=5; $i++) {
$total[$i] += $data[$i];
}
for ($i=6; $i<=9; $$i++) {
if ($data[$i] eq "Yes") { $total[$i]++; }
}

}
}

if ($number_of_records > 0) {
for ($i = 0; $i<=5; $i++) {
$average[$i] = $total[$i] / $number_of_records;
$average[$i] = int($average[$i] + .5);
}
for ($i=6; $i<=9; $$i++) {
$average[$i] = ($total[$i]/$number_of_records)*100;
$average[$i] = int($average[$i] + .5);
}

}
$output = join '|', @average;

I think. :-)

When you load in your averages in html.pl, sub html_record, your percentages will be in $average[6] through $average[9]. You just need to print them out where you want them and add the % sign.

If you're going to copy and paste the code, only copy the bolded sections and put them in the right spot. Otherwise you'll have problems because of the lack of carriage returns on the message board.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Count the yes answers In reply to
Thanks JPDeni,

After updating the db.cgi, and attempting to modify a record, I get the following error:

Modification of a read-only value attempted at db.cgi line 1182 which is the location in the db.cgi of the compute averages file. I believe this is where the Yes/No code starts.

Any ideas?
Quote Reply
Re: [Budb] Count the yes answers In reply to
Not a clue. I've never run into anything that said anything about a "read-only" value.

You might try changing $total[$i]++; to ++$total[$i]; From what I've read it seems that they both should do exactly the same thing, but sometimes one works and sometimes the other works.

I'm going to be away from home from tomorrow morning through Monday evening (Pacific time), with no computer access. Just letting you know that I'm not ignoring you if you have more questions. :-)


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [Budb] Count the yes answers In reply to
Another thing you can try is adding another set of parentheses:

if (($data[$i]) eq "Yes") {

I saw it somewhere. Seems redundant, but maybe it's needed.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Count the yes answers In reply to
Hi all,

After trying the last two suggestions, I still get the same error. Any ideas?
Quote Reply
Re: [Budb] Count the yes answers In reply to
Let me give it a try on my test database. I'll post something about it tomorrow.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [Budb] Count the yes answers In reply to
Can you tell me what your database looks like? I don't know how to make a sample so that I can work out the problem. If you tell me what your fields are, I can more easily figure it out.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Count the yes answers In reply to
Hi JP,
Sure, glad to help.

Here's what you have already done:

Fld No Name Your Answer Avg Graphic

Field 1 Income 2004 $40,000 $52,000 down
Field 2 Income 2005 $50,000 $52,000 down
Field 3 Income 2006 $60,000 $52,000 up
Field 4 Number of kids 5 3 up

And it works very nicely I might add.

I need:
Field 6 Self Employed? Yes ---- 52% yes
Field 7 Have a car? Yes -------- 36% Yes

Where % Yes is the the percentage of the answers that were yes. There is no graphic associated with a yes/no answer. Note: Some could leave these questions blank. No are not calclated.

Thanks
Quote Reply
Re: [Budb] Count the yes answers In reply to
Quote:
Note: Some could leave these questions blank. No are not calclated.


Hmmmm. I'm going to have to figure something out, then.

Let's say you have the following responses to the "self - employed" question:

1: Yes
2: No
3: Yes
4: [blank]
5: No

What would the percentage be? Do we assume that a blank is equal to a "No" or do we not count it? If a blank is equal to a "No," then it would display 40% yes. If a blank is not counted, it would display 50% yes. There's a difference in the way the programming is done.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.
Quote Reply
Re: [JPDeni] Count the yes answers In reply to
The percentage result would be only on those that answered the question so 50% yes would be the correct answer.
Quote Reply
Re: [Budb] Count the yes answers In reply to
Okay. I think I've got it. The reason you were getting the error is that I had made a typo and put two $ on a variable -- $$i. Perl didn't like that.

In db.cgi, sub compute_averages, after

Code:
for ($i = 0; $i<=5; $i++) {
$total[$i] += $data[$i];
}

add

Code:
for ($i=8; $i<=9; $i++) {
if ($data[$i] eq "Yes") {
$total[$i]++;
}
elsif ($data[$i] eq "No") {
$nototal[$i]++;
}
}

Change the 8 and 9 to match the fields where your Yes/No answers are. These are the field numbers as defined in your .cfg file. It's best if you have them contiguous. If you don't, you'll have to use some other way to keep track of which field you're dealing with. You have to be sure they are exactly Yes and No. Use radio buttons instead of text fields to be sure that someone doesn't spell it wrong. (You'd be surprised. :-)

In the same subroutine, after

Quote:
for ($i = 0; $i<=5; $i++) {
$average[$i] = $total[$i] / $number_of_records;
$average[$i] = int($average[$i] + .5);
}

add

Code:
for ($i=8; $i<=9; $i++) {
$nototal[$i] += $total[$i];
$average[$i] = ($total[$i]/$nototal[$i])*100;
$average[$i] = int($average[$i] + .5);
}

Once again, you'll have to change the 8 and 9 to match your field numbers.

In html.pl, sub html_record, use $average[8] and $average[9] (or whatever your field numbers are) for your percentages.


JPD
----------------------------------------------------
JPDeni's DBMan-ual
How to ask questions the smart way.