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";

In reply to string functions by valavanp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.