0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Perlで整数と小数点、ゼロを含む値を数値として判定させる関数

Last updated at Posted at 2018-04-27

※正規表現の箇所は、こちらを参考にしました。

Perlは、数値のゼロを数値として認識してくれないので、判定関数を使って判定すると便利です。

sub is_numeric {

  # pattern     result
  # ------------------
  # 0           TRUE
  # 1           TRUE
  # 123	      TRUE
  # 0.1	      TRUE
  # 0.123	      TRUE
  # 12.345      TRUE
  # 0123	      FALSE
  # 1.a         FALSE
  # 1a          FALSE
  # 1.          FALSE
  # hoge	      FALSE
  # ""          FALSE
  # $undef      FALSE

  my $v = shift;
  
  my $result = 0;
  
  if (defined $v && $v =~ /^([1-9]\d*|0)(\.[0-9]+)?$/) {
    $result = 1;
  }
  
  return $result;
}

コメント部分が長くなってすいませんが。。。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?