my %rules = (A => 1,B => 1,C => 0,D => 1); my $s = 0; while ($F[2] =~ /(\d+)([ABCD])/g) { my $n = $1; my $op = $2; $s += $n * $rules{$op}; } #### $s - Values from D at both ends are added to the total - if D does not exist at both ends in any given code, whichever D is present is added regardless (the way the code currently works) $s1 - Only the value from D at the start of the code is added $s2 - Only the value from D at the end of the code is added #### $s = 47 (2+40+1+4) $s1 = 43 (2+40+1) $s2 = 45 (40+1+4) #### $s = 43 (2+40+1) $s1 = 43 (2+40+1) $s2 = 41 (40+1)