It might be that you are asking either a home work question or the wrong question for a practical application. I am having trouble understanding your application that requires this distinction between C data types.
If you want to know if some string is valid in a numeric context, let Perl figure it out by simply adding "zero" to that number. If that is not a valid number, eg. "1.1.1", you will get a warning error message.
How to intercept this warning message and do something about it is different than what you have asked. Anyway some very simple code is attached that will generate a warning message if a "number" is not a "number"...
#!/usr/bin/perl use strict; use warnings; $| =1; #turn off STDIO buffering #this causes the error messages to STDERR #to appear closer to the standard print stuff my @numbers = qw(1 -1 123.1 0.1 1E6 ); foreach my $number (@numbers) { print "$number: \t", $number+0,"\n"; } print "now weirder stuff...\n\n"; @numbers = qw (1.1.1 4-1-1 --2); foreach my $number (@numbers) { print "$number: \t", $number+0,"\n"; } __END__ OUTPUT: 1: 1 -1: -1 123.1: 123.1 0.1: 0.1 1E6: 1000000 now weirder stuff... Argument "1.1.1" isn't numeric in addition (+) at C:\PerlTemp\testnume +ric2.pl line 19. 1.1.1: 1.1 Argument "4-1-1" isn't numeric in addition (+) at C:\PerlTemp\testnume +ric2.pl line 19. 4-1-1: 4 Argument "--2" isn't numeric in addition (+) at C:\PerlTemp\testnumeri +c2.pl line 19. --2: 0
In reply to Re: How do I determine with a regular expression whether a scalar is a number/whole/integer/float?
by Marshall
in thread [SOLVED] How do I determine with a regular expression whether a scalar is a number/whole/integer/float?
by thanos1983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |