Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: string functions

by Corion (Pope)
on Oct 25, 2006 at 08:54 UTC ( #580512=note: print w/replies, xml ) Need Help??

in reply to string functions

There are many errors or weird things in your program

  1. You are not indenting your code properly so it is very hard to see what loop goes around what code.
  2. 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?
  3. 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.
  4. 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

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://580512]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2021-12-02 11:18 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (18 votes). Check out past polls.