Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^7: How to avoid an alphabet and integer next to it in a string?

by piscean (Acolyte)
on Mar 21, 2014 at 18:52 UTC ( #1079331=note: print w/ replies, xml ) Need Help??


in reply to Re^6: How to avoid an alphabet and integer next to it in a string?
in thread How to avoid an alphabet and integer next to it in a string?

Input was

my $molform = <STDIN>;
C6H9 and Output was:
------Please enter single letter elements in caps only------ Enter the molecular formula of the compound = C6H9 Forbidden chars The molecular weight of the above compound is =


Comment on Re^7: How to avoid an alphabet and integer next to it in a string?
Select or Download Code
Re^8: How to avoid an alphabet and integer next to it in a string?
by hazylife (Monk) on Mar 21, 2014 at 18:55 UTC
    chomp(my $molform = <STDIN>);
      Forget chomp, just do:
      $molform =~ s/\s+$//;
        $molform =~ s/H\d*//g; worked for me except for Hg and He. Yup, I tried $molform =~ s/\s+$//;

        The output is zero

Re^8: How to avoid an alphabet and integer next to it in a string?
by runrig (Abbot) on Mar 21, 2014 at 19:25 UTC
    Think about it. How does that help (read again what output I asked for)? When you're trying to determine input and output to something...just print the something...
    my $molform = <STDIN>; print "Molform before: [$molform]\n"; s/\s+$//, s/H(?![a-z])\d*//g for $molform; print "Molform after: [$molform]\n";
    Although hazylife may have nailed the problem...you need to learn how to provide useful debugging information.
      Using your code, input as C6H9
      ------Please enter single letter elements in caps only------ Enter the molecular formula of the compound = C6H9Hg Molform before: [C6H9 ] Molform after: [C6] The molecular weight of the above compound is = 72.0642

      Using your code, input as C6H9Hg

      ------Please enter single letter elements in caps only------ Enter the molecular formula of the compound = C6H9Hg Molform before: [C6H9Hg ] Molform after: [C6Hg] Molform after: [C6Hg] The molecular weight of the above compound is = 272.6542
      Thanks a ton! Yes, it worked!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (14)
As of 2014-10-23 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (126 votes), past polls