Thread Testen, ob variable eine Zahl ist (21 answers)
Opened by pktm at 2010-02-22 23:21

topeg
 2010-02-23 11:23
#133509 #133509
User since
2006-07-10
2611 articles
BenutzerIn

user image
Um es etwas zu erklären:
Code (perl): (dl )
1
2
3
4
5
sub test_of_number_e
{
  ($_<48 || 57<$_)?return 0:undef() for(unpack('C*',$_[0]));
  return 1;
}


==


Code (perl): (dl )
1
2
3
4
5
sub test_of_number_e
{
  for my $char (unpack('C*',$_[0])){ ($char<48 || 57<$char)?return 0:undef(); }
  return 1;
}


==

Code (perl): (dl )
1
2
3
4
5
sub test_of_number_e
{
  for my $char (unpack('C*',$_[0])){ return 0 if($char<48 || 57<$char); }
  return 1;
}


==

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
sub test_of_number_e
{
  my $number=$_[0];
  for my $char (unpack('C*',$number))
  {
    # 'ASCII 0' = 48
    # 'ASCII 9' = 57 
    return 0 if($char<48 || 57<$char);
  }
  return 1;
}


Nur ist die letzte Variante ist wegen den Zuweisungen 5-10% langsamer.
Last edited: 2010-02-23 17:30:19 +0100 (CET)

View full thread Testen, ob variable eine Zahl ist