Solved

numeric or string ??

Posted on 1998-06-12
10
223 Views
Last Modified: 2010-03-05
Hi,

I would like to know if they have a way to analyse a value receive from a FORM, if it's a numeric number or a string ?

Thank.
0
Comment
Question by:woaini
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1207819
What do you consider a numeric number?
Have you tried the answers in perlfaq4 for "How do I determine whether a scalar is a number/whole/integer/float?"
0
 

Author Comment

by:woaini
ID: 1207820
Hi,

I mean by numeric = number . No, I haven't tried the answer in perlfaq4. Can you tell me where the location is ?

Thank.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1207821
Is "009" a number? "10e"? "10e-999"? "0xFF"? "1/2"? "Q.10057609"? "-inf"? "two"?  "four score and seven"?

perldoc perlfaq4
in the "Data: Misc" section.



0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:woaini
ID: 1207822
0.10 ; 0.50 ; 1 ; 123 are number.
And "yes" or "no" are string.


0
 
LVL 28

Expert Comment

by:sybe
ID: 1207823
Form values are passed as a string, if you want to use them as a number and you are not sure that it can be converted to a number, you should convert it to a number and catch an eventual error.

0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 20 total points
ID: 1207824
beside ozo's comments, would folloing do what you expect?

$_=$string_to_check_if_numeric;
print "$_\n" if m/^[0-9]+\.?$|^[0-9]*\.?[0-9]+$/;
0
 
LVL 84

Expert Comment

by:ozo
ID: 1207825
print /[^\d.]/?"string":"number"; # might suffice for your examples
but it may or may not do what you want for "  .5\n", "+12300.e-2", "01", "", "1e9999", etc.
If you're not sure what you want to do in those cases, or if you're not sure which of the FAQ examples is most suitable for your purposes, perhaps you can tell us what you're trying to do with your values?
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1207826
> beside ozo's comments, would folloing do what you expect?

>     $_=$string_to_check_if_numeric;
>     print "$_\n" if m/^[0-9]+\.?$|^[0-9]*\.?[0-9]+$/;

Probably not, at least in the general case.  Remember that a number may have an ignored underbar (i.e. 9_000 is legit... an Ada-ism).  Also, 4.7e12 is a legitimate number, which would fail your test.

ozo's comments about see the faq are relevant.  Tom covers this question exhaustively (btw, the final answer he gives is really 'you probably don't need to...')

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1207827
b2pi, I know that my answer didn't cover *all* possibilities of writing "numbers", I just want it to be sufficent for woaini.
You even may think about pi, e (==euler), 2/3, and some more which are also legal "numbers" in some contexts.

If you would like to see a more perfect regex, have a look at the "Regualr Expressions" book and you'll see that even in perl it doesn't fit in a single line :-)

BTW, ozo's \d is more perlish than [0-9] (which I like for readability).
0
 

Author Comment

by:woaini
ID: 1207828
Thank a lot !
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question