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";
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.