LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl, integer and floats (https://www.linuxquestions.org/questions/programming-9/perl-integer-and-floats-310489/)

ivanatora 04-06-2005 04:50 PM

Perl, integer and floats
 
How can I check if a number is integer (2 3 123 12122134) or float (1.2 112.56 0.3333) ?
I tried to split it at the decimal point, but the split() function doesn't work with digits (i think):
$test = $i/$n;
@fields = split (/./,$test);
And here @fields is always emty.

puffinman 04-06-2005 04:53 PM

You can split on the decimal point, however, the dot is a meta-character (it actually stands for any character at all), so it must be escaped with a backslash. Try:

Code:

@fields = split (/\./,$test);
However, you don't need to split, you can just test for a match with a regex:

Code:

if ($test ~= m/\./) {
    # test had a decimal point
} else {
    # test did not have a decimal point
}


ivanatora 04-06-2005 05:09 PM

Thanks, I noticed that ;)


All times are GMT -5. The time now is 07:10 PM.