There are many errors or weird things in your program
-
You are not indenting your code properly so it is very hard to see what loop goes around what code.
- Your string expansion is very weird. Why do you expand your string 2A2B2C in a loop
when the regular expression already does the complete expansion?
- Your string length calculation is wrong. You calculate the length of the unexpanded string 2A2B2C instead of calculating the length of the expanded string. Also, name your length variable something better than $b. Maybe call it $string_length or $sequence_length, so that you and others understand what you are doing.
- Your if ... elsif ... structure is very, very bad and error prone. If you want to associate a letter with a value, a hash is the natural structure:
my %value = (
A => 1.5,
B => 2.5,
C => 3.5,
);
my %factor = (
A => 50/100,
B => 55/100,
C => 60/100,
);
You can then easily split up your string and calculate the numbers.
my @letters = split //, $expanded_string;
my $result = 0;
for my $letter (@letters) {
$result = $result + $value{ $letter } * $factor{ $letter };
};
print $result;
With these changes, I get a result of 8.45, so either your calculation of 28.4238 is wrong or you need to explain how you arrive at that number. Basically, my code calculates the following number:
$result = 1.5 * (50/100) * 2 + 2.5 * (55/100) * 2 + 3.5 * (60/100) * 2
-
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.
|