Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi all,
i was trying to figure out how to take a number, check to see if it ends with a decimal point, if so, adds zero after the decimal.
if ($pm25_24_ug == "") { $pm25_24_ug = "N/A"; } elsif ($pm25_24_ug=~/.$/); { $pm25_24_ug = $pm25_24_ug."0"; }
What might I be doing wrong?

Replies are listed 'Best First'.
Re: Newbie regex question
by vroom (His Eminence) on Feb 04, 2000 at 03:02 UTC
    I'll just offer a bit of commentary on the above regex we have to escape the $ with a \ to get \$ so the Perl interpreter won't expect a variable there instead of the literal $. Then we do (.*?) This does a non-greedy match of . (basically anything besides newline) 0 or more times... the question mark makes the previous repeater non-greedy which means it'll match as few characters as it can while still matching your regexp. Then we saw we want to match \* again we escaped because * is a special character and we want it 3 ({3}) of them. The stuff between your first set of parentheses is in $1 so there ya go.
RE: Newbie regex question
by Anonymous Monk on Feb 05, 2000 at 03:53 UTC
    Regexp is a many spendored thing, however it is a bit arcane. Since your example has symbols in it things are a little harder. Especially since $ and * are wild cards in regexp. I use your example to illustrate with.
    $text = q"'$' and '***'"; $text =~ /\$(.*)\*\*\*/; print $1;

    This yeilds: ' and ' The trick is to escape the $ and * with a \ (backslash).

    The q in front of the quotes may look like a typo, but it saves me from having to use a lot of backslashes. Speaking of which, there is a way to not use so many backslashes in our pattern.

    $text =~ /\$(.*)\*{3}/;

    This has the same effect. The regexp is more compact as well. The {} allows me to say how many of the last character to match.

    2001-03-11 Edited by Corion: Added CODE tags

RE: Newbie regex question
by Anonymous Monk on Feb 05, 2000 at 21:08 UTC
    You'd want to do /\$.*?\*\*\*/, assuming you wanted the regexp to be non-greed, and match the first set of *** it finds, rather than the last.
RE: Newbie regex question
by Anonymous Monk on Oct 04, 2000 at 01:23 UTC
    opps,
    looks like i used ; at the end of the elsif.
    i am such a dumbass.
    sorry
Re: Newbie regex question
by mortis (Pilgrim) on Feb 04, 2000 at 02:44 UTC
    something like:
    $text =~ /\$(.*?)\*{3}/; $stuff = $1;