# Constant Sum Question - Problem 1

Dear All,

Q1 is Constant sum question with 5 brands. Total Q1 must be 10 points.

Q2 is Select question and single answer.
=> We need get the code brand with the highest number from Q1.
=> If more than one brands have highest number in Q1 show only those brands and ask.

Q3  is Select question and single answer.
=> We need get the code brand with the second highest number after brand mentioned in Q2.
=> If more than one brands have second highest number in Q1 show only those brands and ask.
=> In Q1, if has one brand got 10 points and then skip Q3.

Thanks.

+1 vote

This is the constructed list instruction that you'll need for Q2 and Q3:

```Begin Unverified Perl
# Parameters
my \$plist = 'list1';
my \$cs = 'Q1';
my \$rank = 1;

# Run
my \$hash = ();
for (my \$i = 1; \$i <= LISTLENGTH(\$plist); \$i++) {
my \$resp = GETVALUE(\$cs . '_' . \$i);
if (!exists \$hash->{\$resp}) {
my @emp = ();
\$hash->{\$resp} = \@emp;
}
push (@{\$hash->{\$resp}}, \$i);
}

my @sortedResps = sort {\$b <=> \$a} keys %{\$hash};
if (scalar @sortedResps >= \$rank) {
my \$resp = \$sortedResps[\$rank - 1];
if (\$resp != 0) {
foreach my \$i (@{\$hash->{\$resp}}) {
}
}
}
End Unverified
```

Line 3 must be updated with the name of the predefined list.  Line 4 must be updated with the name of the constant sum question.  Line 5 should be updated with the value of 2 for Q3.
answered Jun 11, 2019 by Platinum (148,325 points)
selected Jun 12, 2019 by Saroeun
Many thanks Zachary,

Related to above script working well for Q2 but still not match with Q3.
Please let see my 4 examples below:

Example 1:
Q1=> Brand1 = 2 | Brand2 = 2 | Brand3 = 2 | Brand4 = 2 | Brand5 = 2
Q2=> has selected "Brand 2"
Q3=> Display, | Brand1 | Brand3 | Brand4 | Brand5 |

Example 2:
Q1=> Brand1 = 10 | Brand2 = 0 | Brand3 = 0 | Brand4 = 0 | Brand5 = 0
Q2=> Auto selected "Brand 1"

Example 3:
Q1=> Brand1 = 3 | Brand2 = 3 | Brand3 = 3 | Brand4 = 1 | Brand5 = 0
Q2=> has selected "Brand 1"
Q3=> Display, | Brand2 | Brand3

Example 4:
Q1=> Brand1 = 6 | Brand2 = 1 | Brand3 = 1 | Brand4 = 1 | Brand5 = 1
Q2=> Auto selected "Brand 1"
Q3=> Display, | Brand2 | Brand3 | Brand4 | Brand5 |

Thanks
I see.  That makes it a little bit trickier.  Try this instruction for the Q3 list:

```Begin Unverified Perl
# Parameters
my \$plist = 'list1';
my \$cs = 'cs';
my \$skip = GETVALUE('Q2');

# Run
my \$hash = ();
for (my \$i = 1; \$i <= LISTLENGTH(\$plist); \$i++) {
if (\$i == \$skip) {
next;
}
my \$resp = GETVALUE(\$cs . '_' . \$i);
if (!exists \$hash->{\$resp}) {
my @emp = ();
\$hash->{\$resp} = \@emp;
}
push (@{\$hash->{\$resp}}, \$i);
}

my @sortedResps = sort {\$b <=> \$a} keys %{\$hash};
my \$bestResp = \$sortedResps;
if (\$bestResp != 0) {
foreach my \$i (@{\$hash->{\$bestResp}}) {