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

I've got a list that I want to round at a variable granularity, e.g.:
sub round_all { my $interval = shift; my @result; foreach $value (@_) { my $quanta = int(($value + $interval/2) / $interval); push @result, $quanta * $interval; } @result; }
however, not all values in the list are guaranteed to be numbers. If the value is not a number, I want it left intact in the resulting list. Something like:
sub round_all { my $interval = shift; my @result; foreach $value (@_) { if(isnumber($value) { my $quanta = int(($value + $interval/2) / $interval); push @result, $quanta * $interval; } else { push @result, $value; } } @result; }
Is there a simple, non-ugly way to tell what is a number and what isn't?

2006-05-08 Retitled by g0n, as per Monastery guidelines
Original title: 'isnumber() ??'

Replies are listed 'Best First'.
Re: Is there an isnumber()?
by McDarren (Abbot) on May 06, 2006 at 03:06 UTC
    I'd say that looks_like_number from Scalar::Util is probably what you are looking for :)
Re: Is there an isnumber()?
by Zaxo (Archbishop) on May 06, 2006 at 03:09 UTC

    Scalar::Util has looks_like_number(), which does what you want.

    After Compline,
    Zaxo

Re: Is there an isnumber()?
by davidrw (Prior) on May 06, 2006 at 03:15 UTC