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

Hello Monks, I have a rather very easy question: I am reading integers and fractions from a text file. Once I come accross a fraction or an integer with a minus sign in front, I have to be able to recognize and act upon it. How would my if statement be then? I am sure the regex for it must be a very easy one but I just don't know enough perl to do it. Any help would be appreciated. Thanks.
  • Comment on How do I detect if a number has a minus sign?

Replies are listed 'Best First'.
Re: regex
by duff (Parson) on Sep 22, 2004 at 16:15 UTC
Re: regex
by jbware (Chaplain) on Sep 22, 2004 at 16:13 UTC
    This should probably get you started.
    use strict; while (<DATA>) { #OLDmy ($sign, $num, $den) = m@(-)?(\d+)/?(\d*)@; my ($sign, $num, $den) = m@(-)?(\d+)(?:/(\d+))?@; print "sign:$sign numerator:$num denominator:$den\n"; } __DATA__ 23 -35 -23/43 2/3 5/

    -jbWare

    Update:Fixed regex based on ikegami's suggestion. Also pulled / out of the denominator in ikegami's regex.

      slight change:
      m@(-)?(\d+)(?:(/\d+)?)@;
      Yours matched "5/".

      Thanks for your input. However it didn't help much. Let me give some more detail. I read -845, 4/3, and 1.25 into three variables $first, $second, $third. I want to be able to distinguish them: if ("it starts with a minus sign"){do something}; if ("it has / in it (fraction)") {do something}; if ("it contains three digits (may or may not start with minus sign)") {do something}; If it is too hard don't worry. Thanks anyways to all that replied.

        List this?

        foreach (qw( -845 4/3 1.25 )) { /^[-+]?\d{3}$/ && do { print("$_ is a three digit integer, with a possible sign.\n"); #next; }; /^-/ && do { print("$_ starts with a minus sign\n"); #next; }; /\// && do { print("$_ has a / in it.\n"); #next; }; } __END__ output ====== -845 is a three digit integer, with a possible sign. -845 starts with a minus sign 4/3 has a / in it.

        Uncomment the nexts if you want numbers to only match one case.

        Actually, you pretty much have it at this point. $sign, $num & $den contain the info you need. A series of decision statements should handle the rest (how you implement the decision tree is highly dependant on your purpose) so ...
        if($sign eq '-'){ do something ... } if(not $den){ do something .... } # $den will be undefined if no denom +inator is found if(length($num_ == 3){ do something ... }
        There are any number of ways you can identify/validate conditions on your term. (duff's reference has several). The idea is to peel off the parts of your test value that you need into seperate variables and then check the conditions individually. I have found that trying to find some super regex that does it all isn't worth the effort (although it can be aesthetically pleasing)


        PJ
        use strict; use warnings; use diagnostics; (if needed)
Re: How do I detect if a number has a minus sign?
by TedPride (Priest) on Sep 22, 2004 at 22:53 UTC
    To convert a fraction to decimal equivalent (or approximation, if the fraction doesn't divide out cleanly):
    if ($fraction =~ /^((-) ?)?((\d+) )?(\d+) ?\/ ?(\d+)$/) { $fraction = ($2 ? -1 : 1) * ($4 + $5 / $6); }
    To test for a negative:
    if ($fraction =~ /^((-) ?)?((\d+) )?(\d+) ?\/ ?(\d+)$/ && $2) { #run if negative... }