Re: Regular expression: How to determine wether entry is a number?



On Jul 29, 12:21 pm, mykl...@xxxxxxxxx (Aruna Goke) wrote:
Jan-Henrik wrote:
Dear Group,

I'm new to Perl and I have a simple question:

I ask for the entry of a number vie <STDIN>:

----------------------------------------
#!/usr/bin/perl -w
use strict;

my $foo;
print "Enter number: ";
$foo = <STDIN>;
comp($foo);
----------------------------------------

Now I would like to check wether the user really entered a number and
not letters. What would a check like that look like? A regular
expression like this:
----------------------------------------
unless ($foo =~ /[a-zA-Z\D+][^.][\D*]/ {...};
----------------------------------------

Is there an easier or more beautiful way?

Also, how would I substract just a number from a string? Searched the
net for an example but didn't succeed, so sorry for asking a question
like that...

Many thanks for your help!

Kind regards,

Jan-Henrik

#!/usr/bin/perl

use warnings;
use strict;

my $foo;
my $num = 3;
print "Enter number: ";
$foo = <STDIN>;
chomp($foo);

print $foo - $num unless $foo =~ /[a-zA-Z]+/;- Hide quoted text -

... and if I enter the string "$*(!)!! ;'.,.<<>", what would you
expect to happen?

.



Relevant Pages

  • Re: array iteration problem
    ... $hash++ for @letters; ... Also since strict and warnings have been recommended I might also poitin outthat %hash is a *very* bad name for a hash, its very ambiguouse and hard to maintain, The entire thing is clearly written as: ...
    (perl.beginners)
  • Re: An actual coding question
    ... use strict; ... my $num = 0; ... sub CONF_VAR1 { ... return pop @letters; ...
    (perl.beginners)
  • Charlene might little enhance our doll
    ... Ella, have a continuing explosion. ... I apply wooden pardons amid the teenage strict circuit, ... thanking by lesser Aloysius until his accusation races upstairs. ... It can unfortunately wait real and allocates our regular, ...
    (sci.crypt)
  • Re: Help with syntax
    ... to have forgotten some of the subtelties of the language including how ... I'm using strict here to help catch ... print "regular file\n" ...
    (comp.lang.perl.misc)
  • Re: Gutes JavaScript Buch
    ... mancher Regular hier, ... wie viel Grundwissen aneigne. ... evangelism: XHTML 1.0 Strict ...
    (de.comp.lang.javascript)