http://www.perlmonks.org?node_id=580509

valavanp has asked for the wisdom of the Perl Monks concerning the following question:

I have a string. $a="2A2B2C". The values of A,B,C are stored in an array @p=(1.5,2.5,3.5). The result of $a should be like this: (AA)*(50/100) (BB)*(55/100) (CC)*(60/100). The values of A,B,C should be repeated according to the adjacent number before that character. I may be wrong somewhere in the coding. The answer should be 28.4238 where as i am getting 2.165625. Here is the code which i used:
$a="2A2B2C"; @p=(1.5,2.5,3.5); #print substr($a,0,1); #print "\n"; $b=length($a); print "$b\n"; for($i=0;$i<$b;$i++){ $a=~s/(\d+)(.)/$2 x $1/ge; } #print $a; for(1..$b) { if(1==$_) { $c=substr($a,0,1); $r1=$p[0]*(50/100); print $r1; } elsif(2==$_) { $c2=substr($a,1,1); #print "\n$c"; } elsif(3==$_) { $c3=substr($a,2,1); $r2=$p[1]*(55/100); print "\n$r2"; } elsif(4==$_) { $c4=substr($a,3,1); #print "\n$c"; } elsif(5==$_) { $c5=substr($a,3,1); $r3=$p[2]*(60/100); #print "\n$r3"; } elsif(6==$_) { $c6=substr($a,5,1); #print "\n$c"; } } $answer=$r1*$r2*$r3; print "\n$answer";