Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

conjoint cards picking by using least quota`

Hi i have a requirement to pick the cards by using the least quota. Here i have designed with 4 card quota, each card consists of 14 cards in it. Based on the least count each card will be picked. I have written a lengthy code it . Is there any way of  making code to reduce

Begin Unverified Perl
 my $c1a1=999;
 my $c1a2=999;
 my $c1a3=999;
 my $c1a4=999;
 my $c1a5=999;
 my $c1a6=999;
 my $c1a7=999;
 my $c1a8=999;
 my $c1a9=999;
 my $c1a10=999;
 my $c1a11=999;
 my $c1a12=999;
 my $c1a13=999;
 my $c1a14=999;
 
 my $c2a1=999;
 my $c2a2=999;
 my $c2a3=999;
 my $c2a4=999;
 my $c2a5=999;
 my $c2a6=999;
 my $c2a7=999;
 my $c2a8=999;
 my $c2a9=999;
 my $c2a10=999;
 my $c2a11=999;
 my $c2a12=999;
 my $c2a13=999;
 my $c2a14=999;
 
 my $c3a1=999;
 my $c3a2=999;
 my $c3a3=999;
 my $c3a4=999;
 my $c3a5=999;
 my $c3a6=999;
 my $c3a7=999;
 my $c3a8=999;
 my $c3a9=999;
 my $c3a10=999;
 my $c3a11=999;
 my $c3a12=999;
 my $c3a13=999;
 my $c3a14=999;
 
 
 my $c4a1=999;
 my $c4a2=999;
 my $c4a3=999;
 my $c4a4=999;
 my $c4a5=999;
 my $c4a6=999;
 my $c4a7=999;
 my $c4a8=999;
 my $c4a9=999;
 my $c4a10=999;
 my $c4a11=999;
 my $c4a12=999;
 my $c4a13=999;
 my $c4a14=999;
 
$c1a1=QUOTACELLCOMPLETES("CardQuota1",1);
$c1a2=QUOTACELLCOMPLETES("CardQuota1",2);
$c1a3=QUOTACELLCOMPLETES("CardQuota1",3);
$c1a4=QUOTACELLCOMPLETES("CardQuota1",4);
$c1a5=QUOTACELLCOMPLETES("CardQuota1",5);
$c1a6=QUOTACELLCOMPLETES("CardQuota1",6);
$c1a7=QUOTACELLCOMPLETES("CardQuota1",7);
$c1a8=QUOTACELLCOMPLETES("CardQuota1",8);
$c1a9=QUOTACELLCOMPLETES("CardQuota1",9);
$c1a10=QUOTACELLCOMPLETES("CardQuota1",10);
$c1a11=QUOTACELLCOMPLETES("CardQuota1",11);
$c1a12=QUOTACELLCOMPLETES("CardQuota1",12);
$c1a13=QUOTACELLCOMPLETES("CardQuota1",13);
$c1a14=QUOTACELLCOMPLETES("CardQuota1",14);


$c2a1=QUOTACELLCOMPLETES("CardQuota2",1);
$c2a2=QUOTACELLCOMPLETES("CardQuota2",2);
$c2a3=QUOTACELLCOMPLETES("CardQuota2",3);
$c2a4=QUOTACELLCOMPLETES("CardQuota2",4);
$c2a5=QUOTACELLCOMPLETES("CardQuota2",5);
$c2a6=QUOTACELLCOMPLETES("CardQuota2",6);
$c2a7=QUOTACELLCOMPLETES("CardQuota2",7);
$c2a8=QUOTACELLCOMPLETES("CardQuota2",8);
$c2a9=QUOTACELLCOMPLETES("CardQuota2",9);
$c2a10=QUOTACELLCOMPLETES("CardQuota2",10);
$c2a11=QUOTACELLCOMPLETES("CardQuota2",11);
$c2a12=QUOTACELLCOMPLETES("CardQuota2",12);
$c2a13=QUOTACELLCOMPLETES("CardQuota2",13);
$c2a14=QUOTACELLCOMPLETES("CardQuota2",14);
 
$c3a1=QUOTACELLCOMPLETES("CardQuota3",1);
$c3a2=QUOTACELLCOMPLETES("CardQuota3",2);
$c3a3=QUOTACELLCOMPLETES("CardQuota3",3);
$c3a4=QUOTACELLCOMPLETES("CardQuota3",4);
$c3a5=QUOTACELLCOMPLETES("CardQuota3",5);
$c3a6=QUOTACELLCOMPLETES("CardQuota3",6);
$c3a7=QUOTACELLCOMPLETES("CardQuota3",7);
$c3a8=QUOTACELLCOMPLETES("CardQuota3",8);
$c3a9=QUOTACELLCOMPLETES("CardQuota3",9);
$c3a10=QUOTACELLCOMPLETES("CardQuota3",10);
$c3a11=QUOTACELLCOMPLETES("CardQuota3",11);
$c3a12=QUOTACELLCOMPLETES("CardQuota3",12);
$c3a13=QUOTACELLCOMPLETES("CardQuota3",13);
$c3a14=QUOTACELLCOMPLETES("CardQuota3",14);
 
$c4a1=QUOTACELLCOMPLETES("CardQuota4",1);
$c4a2=QUOTACELLCOMPLETES("CardQuota4",2);
$c4a3=QUOTACELLCOMPLETES("CardQuota4",3);
$c4a4=QUOTACELLCOMPLETES("CardQuota4",4);
$c4a5=QUOTACELLCOMPLETES("CardQuota4",5);
$c4a6=QUOTACELLCOMPLETES("CardQuota4",6);
$c4a7=QUOTACELLCOMPLETES("CardQuota4",7);
$c4a8=QUOTACELLCOMPLETES("CardQuota4",8);
$c4a9=QUOTACELLCOMPLETES("CardQuota4",9);
$c4a10=QUOTACELLCOMPLETES("CardQuota4",10);
$c4a11=QUOTACELLCOMPLETES("CardQuota4",11);
$c4a12=QUOTACELLCOMPLETES("CardQuota4",12);
$c4a13=QUOTACELLCOMPLETES("CardQuota4",13);
$c4a14=QUOTACELLCOMPLETES("CardQuota4",14);

 my @c1a_val;
 my @c2a_val;
 my @c3a_val;
 my @c4a_val;
 
 $c1a_val[0][0]= $c1a1;
 $c1a_val[1][0]= $c1a2;
 $c1a_val[2][0]= $c1a3;
 $c1a_val[3][0]= $c1a4;
 $c1a_val[4][0]= $c1a5;
 $c1a_val[5][0]= $c1a6;
 $c1a_val[6][0]= $c1a7;
 $c1a_val[7][0]= $c1a8;
 $c1a_val[8][0]= $c1a9;
 $c1a_val[9][0]= $c1a10;
 $c1a_val[10][0]= $c1a11;
 $c1a_val[11][0]= $c1a12;
 $c1a_val[12][0]= $c1a13;
 $c1a_val[13][0]= $c1a14;
 
 $c1a_val[0][1]="1";
 $c1a_val[1][1]="2";
 $c1a_val[2][1]="3";
 $c1a_val[3][1]="4";
 $c1a_val[4][1]="5";
 $c1a_val[5][1]="6";
 $c1a_val[6][1]="7";
 $c1a_val[7][1]="8";
 $c1a_val[8][1]="9";
 $c1a_val[9][1]="10";
 $c1a_val[10][1]="11";
 $c1a_val[11][1]="12";
 $c1a_val[12][1]="13";
 $c1a_val[13][1]="14";
 
 $c2a_val[0][0]= $c2a1;
 $c2a_val[1][0]= $c2a2;
 $c2a_val[2][0]= $c2a3;
 $c2a_val[3][0]= $c2a4;
 $c2a_val[4][0]= $c2a5;
 $c2a_val[5][0]= $c2a6;
 $c2a_val[6][0]= $c2a7;
 $c2a_val[7][0]= $c2a8;
 $c2a_val[8][0]= $c2a9;
 $c2a_val[9][0]= $c2a10;
 $c2a_val[10][0]= $c2a11;
 $c2a_val[11][0]= $c2a12;
 $c2a_val[12][0]= $c2a13;
 $c2a_val[13][0]= $c2a14;
 
 $c2a_val[0][1]="1";
 $c2a_val[1][1]="2";
 $c2a_val[2][1]="3";
 $c2a_val[3][1]="4";
 $c2a_val[4][1]="5";
 $c2a_val[5][1]="6";
 $c2a_val[6][1]="7";
 $c2a_val[7][1]="8";
 $c2a_val[8][1]="9";
 $c2a_val[9][1]="10";
 $c2a_val[10][1]="11";
 $c2a_val[11][1]="12";
 $c2a_val[12][1]="13";
 $c2a_val[13][1]="14";
 
 
 $c3a_val[0][0]= $c3a1;
 $c3a_val[1][0]= $c3a2;
 $c3a_val[2][0]= $c3a3;
 $c3a_val[3][0]= $c3a4;
 $c3a_val[4][0]= $c3a5;
 $c3a_val[5][0]= $c3a6;
 $c3a_val[6][0]= $c3a7;
 $c3a_val[7][0]= $c3a8;
 $c3a_val[8][0]= $c3a9;
 $c3a_val[9][0]= $c3a10;
 $c3a_val[10][0]= $c3a11;
 $c3a_val[11][0]= $c3a12;
 $c3a_val[12][0]= $c3a13;
 $c3a_val[13][0]= $c3a14;
 
 $c3a_val[0][1]="1";
 $c3a_val[1][1]="2";
 $c3a_val[2][1]="3";
 $c3a_val[3][1]="4";
 $c3a_val[4][1]="5";
 $c3a_val[5][1]="6";
 $c3a_val[6][1]="7";
 $c3a_val[7][1]="8";
 $c3a_val[8][1]="9";
 $c3a_val[9][1]="10";
 $c3a_val[10][1]="11";
 $c3a_val[11][1]="12";
 $c3a_val[12][1]="13";
 $c3a_val[13][1]="14";
 
 $c4a_val[0][0]= $c4a1;
 $c4a_val[1][0]= $c4a2;
 $c4a_val[2][0]= $c4a3;
 $c4a_val[3][0]= $c4a4;
 $c4a_val[4][0]= $c4a5;
 $c4a_val[5][0]= $c4a6;
 $c4a_val[6][0]= $c4a7;
 $c4a_val[7][0]= $c4a8;
 $c4a_val[8][0]= $c4a9;
 $c4a_val[9][0]= $c4a10;
 $c4a_val[10][0]= $c4a11;
 $c4a_val[11][0]= $c4a12;
 $c4a_val[12][0]= $c4a13;
 $c4a_val[13][0]= $c4a14;
 
 $c4a_val[0][1]="1";
 $c4a_val[1][1]="2";
 $c4a_val[2][1]="3";
 $c4a_val[3][1]="4";
 $c4a_val[4][1]="5";
 $c4a_val[5][1]="6";
 $c4a_val[6][1]="7";
 $c4a_val[7][1]="8";
 $c4a_val[8][1]="9";
 $c4a_val[9][1]="10";
 $c4a_val[10][1]="11";
 $c4a_val[11][1]="12";
 $c4a_val[12][1]="13";
 $c4a_val[13][1]="14";
 
 
 my @c1a_min;
 @c1a_min=sort{$a->[0]<=>$b->[0]}@c1a_val;
 
 
  my @c2a_min;
 @c2a_min=sort{$a->[0]<=>$b->[0]}@c2a_val;

 
  my @c3a_min;
 @c3a_min=sort{$a->[0]<=>$b->[0]}@c3a_val;

  
  my @c4a_min;
 @c4a_min=sort{$a->[0]<=>$b->[0]}@c4a_val;

 
 my $x1;
 my $x2;
 my $x3;
 
 if($c1a_min[0][1]==$c2a_min[0][1])
 {
     $x1=$c2a_min[1][1];
 }
 else
 {
     $x1=$c2a_min[0][1];
 }
 
 for (my $i=0;$i<=3;$i++)
 {
     if($c1a_min[0][1]==$c2a_min[$i][1] ||$x1==$c2a_min[$i][1])
    {
    }
    else
    {
    $x2=$c2a_min[$i][1];
    $i=4;
    }
}

 for(my $i=0;$i<=4;$i++)
 {
     if($c1a_min[0][1]==$c4a_min[$i][1] ||$x1==$c4a_min[$i][1] ||$x2==$c4a_min[$i][1])
    {
    }
    else
    {
    $x3=$c4a_min[$i][1];
    $i=4;
    }
}

ADD("Cardlist",$c1a_min[0][1]);
ADD("Cardlist",$x1);
ADD("Cardlist",$x2);
ADD("Cardlist",$x3);

End Unverified
asked Mar 2, 2017 by sandeepkapalawai Bronze (1,475 points)
retagged Mar 2, 2017 by Walter Williams

1 Answer

0 votes
I'm glad you asked.  Code reviews like this can be a great way to learn new coding tricks.

The first thing that jumps out to me is that you have a number of "middle man" lines of code that you can cut out.  For example, these three lines appear in your code:

my $c1a1 = 999;
$c1a1 = QUOTACELLCOMPLETES("CardQuota1", 1);
$c1a_val[0][0] = $c1a1;


You can remove the first two lines and just use this in place of the third line:

$c1a_val[0][0] = QUOTACELLCOMPLETES("CardQuota1", 1);


Second, you've got a lot of code that could be more easily done with a loop.  Here's lines 144-157 done with a for loop:

for (my $i = 0; $i <= 13; $i++) {
    $c1a_val[$i][1] = $i + 1;
}


Combining those two improvements, you can get the entirety of defining @c1a_val down to five lines of code:

my @c1a_val;
for (my $i = 0; $i <= 13; $i++) {
    $c1a_val[$i][0] = QUOTACELLCOMPLETES('CardQuota1'. $i + 1);
    $c1a_val[$i][1] = $i + 1;
}


Doing that for c1a, c2a, c3a, and c4a should be a huge code reduction.  Beyond that, I could just offer a few miscellaneous tips.

The sortings can be done with one line instead of two:

my @c1a_min = sort{$a->[0]<=>$b->[0]} @c1a_val;


x1 could be defined with a one-line ternary operator rather than with the if-else:

my $x1 = ($c1a_min[0][1] == $c2a_min[0][1]) ? $c2a_min[1][1] : $c2a_min[0][1];


Instead of having an empty if then a non-empty else, you can rework the code to just need the if.  Recall that "not (A or B or C)" is equivalent to "(not A) and (not B) and (not C)."

if ($c1a_min[0][1] != $c2a_min[$i][1] && $x1 != $c2a_min[$i][1]) {
    $x2 = $c2a_min[$i][1];
    $i = 4;
}


if ($c1a_min[0][1] != $c4a_min[$i][1] && $x1 != $c4a_min[$i][1] && $x2 != $c4a_min[$i][1]) {
    $x3 = $c4a_min[$i][1];
    $i = 4;
}


Finally, lines 288 and 300 could be replaced with this line of code to immediately end the for loop:

last;
answered Mar 2, 2017 by Zachary Platinum Sawtooth Software, Inc. (95,875 points)
...