Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by hippo (Deacon)
on Mar 21, 2014 at 17:53 UTC ( #1079312=note: print w/ replies, xml ) Need Help??


in reply to How to avoid an alphabet and integer next to it in a string?

I tried doing that, but I failed.

What did you try? How did it fail?

Hard to tell, but is all you are looking for this?

my $formula = 'C6H5OH'; $formula =~ s/H\d//g; print "$formula\n";

Update: As runrig suggests below the more general s/H\d*//g; may be more appropriate to your needs.


Comment on Re: How to avoid an alphabet and integer next to it in a string?
Select or Download Code
Re^2: How to avoid an alphabet and integer next to it in a string?
by piscean (Acolyte) on Mar 21, 2014 at 17:57 UTC
    Yes, this is what I was looking for. Thanks! I tried avoiding H in C6H9. It turned out to calculate C69 giving me a wrong result. Of course, I was foolish enough to try this.
      my $molform = <STDIN>; $molform =~ s/[^a-zA-G0-9]//g; my $molmass = new Chemistry::MolecularMass; my $mass = $molmass->calc_mass("$molform");
        $molform =~ s/[^a-zA-G0-9]//g;

        Just eliminate the Hydrogen:

        $molform =~ s/H\d*//g;
Re^2: How to avoid an alphabet and integer next to it in a string?
by piscean (Acolyte) on Mar 21, 2014 at 18:05 UTC
    Oops! This is giving me wrong output too. Hope the code I posted below gives an idea of what I wanted.
      What about this?
      $formula =~ s/H\d*//g;
        Nope :( If the molecular formula entered is C6H9, the code should give an output equal to 12.01*6=72.06. After applying this, it's giving me 12.01*69=828.69

        The Problem with s/H\d*//g is that it will also remove the H from HgS (ok,, not organic chemistry, but you get the point).

        One way to avoid that is to use a negative look-ahead:

        s/H(?![a-z])\d*//</c> <p>Or if you prefer the Unicode-y approach:</p> <code>s/H(?!\p{Ll})\d*//
Re^2: How to avoid an alphabet and integer next to it in a string?
by piscean (Acolyte) on Mar 21, 2014 at 19:17 UTC
    As runrig suggests below the more general s/H\d*//g; may be more appropriate to your needs.

    It does help, but what about Hg element? When I enter Hg, it gives me zero as output.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-07-30 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (237 votes), past polls