Re: Test if string is a number?

2005-06-30 Thread $Bill Luebkert
Lyle Kopnicky wrote:

 Thanks folks.  I think I'll go with looks_like_number from 
 Scalar::Util.  I like to use library routines where possible.  I don't 
 know how I overlooked that, since I poked through Scalar::Util earlier.
 
 It just seems bizarre to me that something like that isn't a builtin.  I 
 mean, you can't even tell strings from numbers?  I'm used to working in 
 strongly typed languages.

If you always are careful to put things away that you know what they
are, it's not necessary.  IE: before you store some external piece
of data - determine exactly what it is and format it in a manner that
you don't have bother worrying about later.

So when something first arrives in a vrbl, that's the time to get it
into a strict format so you needn't worry about it later.

-- 
  ,-/-  __  _  _ $Bill LuebkertMailto:[EMAIL PROTECTED]
 (_/   /  )// //   DBE CollectiblesMailto:[EMAIL PROTECTED]
  / ) /--  o // //  Castle of Medieval Myth  Magic http://www.todbe.com/
-/-' /___/__/_/_http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Test if string is a number?

2005-06-30 Thread Chris Wagner
Wow there's been a lot of heavy duty code proposed to do something so
simple.  The answer is in how Perl converts between the two.

print is a number if $var eq $var + 0;
print not a number if $var ne $var + 0;

Say $var is bob.  In the first case we see if bob is string equal to bob
+ 0 or is bob eq 0.  Obviously not.
Say $var is 5.  In the second case we see if 5 is not string equal to 5 +
0 or is 5 ne 5.  

In this setup we're forcing the variable into numeric context and then back
into string context.  How the variable survives that procedure depends on
whether it is number like or not number like.

At 08:31 PM 6/29/05 -0700, Lyle Kopnicky wrote:
Thanks folks.  I think I'll go with looks_like_number from 
Scalar::Util.  I like to use library routines where possible.  I don't 
know how I overlooked that, since I poked through Scalar::Util earlier.

It just seems bizarre to me that something like that isn't a builtin.  I 
mean, you can't even tell strings from numbers?  I'm used to working in 
strongly typed languages.






--
REMEMBER THE WORLD TRADE CENTER ---= WTC 911 =--
...ne cede males

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Test if string is a number?

2005-06-30 Thread Thomas, Mark - BLS CTR
 Wow there's been a lot of heavy duty code proposed to do something so
 simple.  The answer is in how Perl converts between the two.
 
 print is a number if $var eq $var + 0;
 print not a number if $var ne $var + 0;

That fails on 1e7.

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Test if string is a number?

2005-06-30 Thread Joe Discenza
Title: Re: Test if string is a number?






Chris Wagner wrote, on Thu 6/30/2005 08:48

: Wow there's been a lot of heavy duty code proposed to do 
something so: simple. The answer is in how Perl converts between the 
two.:: print "is a number" if $var eq $var + 0;: print "not a 
number" if $var ne $var + 0;
Except if $var is, say, '0.00'. Then $var + 0 is '0', and 
won't eq $var.

Joe
== 
Joseph P. Discenza, Sr. 
Programmer/Analyst 
mailto:[EMAIL PROTECTED] 
Carleton Inc. http://www.carletoninc.com 
574.243.6040 ext. 300 fax: 574.243.6060Providing 
Financial Solutions and Compliance for over 30 
Years


___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Test if string is a number?

2005-06-30 Thread $Bill Luebkert
Chris Wagner wrote:
 Wow there's been a lot of heavy duty code proposed to do something so
 simple.  The answer is in how Perl converts between the two.
 
 print is a number if $var eq $var + 0;
 print not a number if $var ne $var + 0;
 
 Say $var is bob.  In the first case we see if bob is string equal to bob
 + 0 or is bob eq 0.  Obviously not.
 Say $var is 5.  In the second case we see if 5 is not string equal to 5 +
 0 or is 5 ne 5.  
 
 In this setup we're forcing the variable into numeric context and then back
 into string context.  How the variable survives that procedure depends on
 whether it is number like or not number like.

Some of us use strict and warnings.  What happens with this ? :

use strict;
use warnings;
my $var = undef;
print is a number if $var eq $var + 0;
print not a number if $var ne $var + 0;   

and

my $var = 'bob';
print is a number if $var eq $var + 0;# will give a warning if $var 
not a number

-- 
  ,-/-  __  _  _ $Bill LuebkertMailto:[EMAIL PROTECTED]
 (_/   /  )// //   DBE CollectiblesMailto:[EMAIL PROTECTED]
  / ) /--  o // //  Castle of Medieval Myth  Magic http://www.todbe.com/
-/-' /___/__/_/_http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Test if string is a number?

2005-06-30 Thread Siebe Tolsma
Title: Re: Test if string is a number?



How about regexp?

/^\-?(\d+\.?\d*|\.\d+)$/

  - Original Message - 
  From: 
  Joe 
  Discenza 
  To: Chris Wagner ; perl-win32-users 
  
  Sent: Thursday, June 30, 2005 4:48 
  PM
  Subject: RE: Test if string is a 
  number?
  
  
  Chris Wagner wrote, on Thu 6/30/2005 08:48
  
  : Wow there's been a lot of heavy duty code proposed to do 
  something so: simple. The answer is in how Perl converts between the 
  two.:: print "is a number" if $var eq $var + 0;: print "not a 
  number" if $var ne $var + 0;
  Except if $var is, say, '0.00'. Then $var + 0 is '0', and 
  won't eq $var.
  
  Joe
  == 
  Joseph P. Discenza, Sr. 
  Programmer/Analyst 
  mailto:[EMAIL PROTECTED] 
  Carleton Inc. http://www.carletoninc.com 
  574.243.6040 ext. 300 fax: 
  574.243.6060Providing Financial Solutions and Compliance for 
  over 30 Years
  
  

  ___Perl-Win32-Users 
  mailing listPerl-Win32-Users@listserv.ActiveState.comTo unsubscribe: 
  http://listserv.ActiveState.com/mailman/mysubs
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Test if string is a number?

2005-06-30 Thread Andreas.Kamentz


For a long time I'm using the function below to test for a number, being
quite satisfied with it ...


# (nmb) number
# Returns decimal value of the contents if argument
# is a number (integer if octal or hexadecimal),
# otherwise returns empty string ('')

sub number($) {
local $_ = shift; s/^\s*(.*)\s*$/$1/;
return '' unless
/^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$|^[+-]?0x[0-9a-fA-F]+$/;
s/\+//; my $sign = s/^-// ? '-' : '';
/^0[0-7]+$|^0x[0-9a-fA-F]+$/ ? $sign.oct : $sign.$_*1;
}

Greetings

 -- Andreas
__


Delphi Electronics  Safety FUBA Reception Systems

Andreas Kamentz
Electrical Engineer / SW
Antenna Engineering
[EMAIL PROTECTED]
Tel: (+49) 5063.990.541
Fax: (+49) 5063.990.99541

Mailing address
FUBA Automotive GmbH  Co. KG
TecCenter
D-31162 Bad Salzdetfurth / Germany

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Siebe Tolsma
Sent: Thursday, June 30, 2005 5:31 PM
To: Joe Discenza; perl-win32-users@listserv.ActiveState.com
Subject: Re: Test if string is a number?


How about regexp?

/^\-?(\d+\.?\d*|\.\d+)$/
- Original Message -
From: Joe Discenza
To: Chris Wagner ; perl-win32-users
Sent: Thursday, June 30, 2005 4:48 PM
Subject: RE: Test if string is a number?


Chris Wagner wrote, on Thu 6/30/2005 08:48

: Wow there's been a lot of heavy duty code proposed to do something so
: simple.  The answer is in how Perl converts between the two.
:
: print is a number if $var eq $var + 0;
: print not a number if $var ne $var + 0;
Except if $var is, say, '0.00'. Then $var + 0 is '0', and won't eq $var.

Joe
==
  Joseph P. Discenza, Sr. Programmer/Analyst
   mailto:[EMAIL PROTECTED]

  Carleton Inc.   http://www.carletoninc.com
  574.243.6040 ext. 300fax: 574.243.6060

Providing Financial Solutions and Compliance for over 30 Years



___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



Note: The information contained in this message may be privileged and 
confidential and thus protected from disclosure. If the reader of this message 
is not the intended recipient, or an employee or agent responsible for 
delivering this message to the intended recipient, you are hereby notified that 
any dissemination, distribution or copying of this communication is strictly 
prohibited. If you have received this communication in error, please notify us 
immediately by replying to the message and deleting it from your computer. 
Thank you.



___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Test if string is a number?

2005-06-30 Thread Chris Wagner
At 09:48 AM 6/30/05 -0500, Joe Discenza wrote:
Except if $var is, say, '0.00'. Then $var + 0 is '0', and won't eq $var.

0.00 is not a valid internal representation of a number.  That can only
exist as a string.  Same goes for 1e7.  That is a print formated number,
not a valid internal number.  $var = 1e7 and print $var - 1000. $var =
0.00 and print $var - 0.  If u want to include numberish strings then u
need some eval's to digest the various number formats.  ... if eval $var eq
$var + 0;





--
REMEMBER THE WORLD TRADE CENTER ---= WTC 911 =--
...ne cede males

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Test if string is a number?

2005-06-30 Thread Thomas, Mark - BLS CTR
 
[EMAIL PROTECTED] wrote:
 0.00 is not a valid internal representation of a number.  
 That can only exist as a string. 

I think u need to re-read the subject of this thread.

- Mark.

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Test if string is a number?

2005-06-30 Thread Joe Discenza
Title: RE: Test if string is a number?






Chris Wagner wrote, on Thu 6/30/2005 12:41

: At 09:48 AM 6/30/05 -0500, Joe Discenza wrote:: Except 
if $var is, say, '0.00'. Then $var + 0 is '0', and won't eq $var.:: 0.00 
is not a valid internal representation of a number. That can only: 
exist as a string. Same goes for "1e7". That is a print formated 
number,: not a valid internal number. $var = 1e7 and print $var - 
1000. $var =: 0.00 and print $var - 0. If u want to include 
"numberish" strings then u: need some eval's to digest the various number 
formats. ... if eval $var eq: $var + 0;
IIRC (no longer have the message), the OP had *string* data to 
check for numerics. Your method would miss, say, a CSV full of prices if they 
ended ".00" or ".50".
I bet you're right that "eval($var) eq $var + 0" works; have you benchmarked 
it against all the other (regex, e.g.) methods presented?
Joe


== 
Joseph P. Discenza, Sr. 
Programmer/Analyst 
mailto:[EMAIL PROTECTED] 
Carleton Inc. http://www.carletoninc.com 
574.243.6040 ext. 300 fax: 574.243.6060Providing 
Financial Solutions and Compliance for over 30 Years



___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Test if string is a number?

2005-06-30 Thread Chris Wagner
At 12:16 PM 6/30/05 -0500, Joe Discenza wrote:
I bet you're right that eval($var) eq $var + 0 works; have you
benchmarked it against all the other (regex, e.g.) methods presented?

I haven't benchmarked it but I can garuntee that it's faster than a regex.
Anything's faster than that. ;)  This should cover everything:


foreach $var (1e7, 0, 5, 6, 0.00, -4, abc, f4fc) {
if ($var eq $var + 0) {
print $var is a pure number\n; 
}
elsif (eval $var eq $var + 0) {
print $var is a number\n 
}
else {
print $var is not a number\n;
}
}


1e7 is a number
0 is a pure number
5 is a pure number
6 is a pure number
0.00 is a number
-4 is a pure number
abc is not a number
f4fc is not a number







--
REMEMBER THE WORLD TRADE CENTER ---= WTC 911 =--
...ne cede males

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Test if string is a number?

2005-06-30 Thread Joe Discenza
Title: RE: Test if string is a number?






Chris Wagner wrote, on Thu 6/30/2005 14:41

: At 12:16 PM 6/30/05 -0500, Joe Discenza wrote:: I bet 
you're right that "eval($var) eq $var + 0" works; have you: benchmarked 
it against all the other (regex, e.g.) methods presented?:: I haven't 
benchmarked it but I can garuntee that it's faster than a regex.: Anything's 
faster than that. ;) This should cover everything:
Thanks for playing. I compared your function with a regex I 
whipped up (/^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/,which gave the same 
results, except for distinguishing "pure" numbers, on your dataset): 
Benchmark: timing 10 iterations of chris, 
regex... chris: 12 wallclock secs (11.86 usr + 
0.00 sys = 11.86 CPU) @ 8432.41/s (n=10) 
regex: 2 wallclock secs ( 1.56 usr + 0.00 sys = 1.56 CPU) @ 
63979.53/s (n=10)
Regex is pretty fast. Eval is usually pretty slow.
Joe

== 
Joseph P. Discenza, Sr. 
Programmer/Analyst 
mailto:[EMAIL PROTECTED] 
Carleton Inc. http://www.carletoninc.com 
574.243.6040 ext. 300 fax: 574.243.6060Providing 
Financial Solutions and Compliance for over 30 Years



___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Test if string is a number?

2005-06-30 Thread $Bill Luebkert
Joe Discenza wrote:

 Chris Wagner wrote, on Thu 6/30/2005 14:41
 
 : At 12:16 PM 6/30/05 -0500, Joe Discenza wrote:
 : I bet you're right that eval($var) eq $var + 0 works; have you
 : benchmarked it against all the other (regex, e.g.) methods presented?
 :
 : I haven't benchmarked it but I can garuntee that it's faster than a regex.
 : Anything's faster than that. ;)  This should cover everything:
 
 Thanks for playing. I compared your function with a regex I whipped up
 (/^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/, which gave the same
 results, except for distinguishing pure numbers, on your dataset):
 
 Benchmark: timing 10 iterations of chris, regex...
  chris: 12 wallclock secs (11.86 usr +  0.00 sys = 11.86 CPU) @
 8432.41/s (n=10)
  regex:  2 wallclock secs ( 1.56 usr +  0.00 sys =  1.56 CPU) @
 63979.53/s (n=10)
 
 Regex is pretty fast. Eval is usually pretty slow.

1) I don't consider Chris's solution to be a legit solution until it
doesn't issue any warnings or error messages.

2) I got these numbers from my test (strict/warnings turned off) :

  Rate RE STRTOD IFLLN
RE  7530/s --   -72%   -75%   -75%
STRTOD 26948/s   258% --   -10%   -11%
IF 29782/s   295%11% ---2%
LLN30274/s   302%12% 2% --

looks_like_number seems to be the winner.

I used the same two lines of code in each sub to clean up
the input:
$var =~ s/^\s+//; $var =~ s/\s+$//;
return 0 if not defined $var or $var eq '';

use strict;
use warnings;
use Scalar::Util qw(looks_like_number);
use POSIX qw(strtod);
use Benchmark qw(timethese cmpthese);

# test output of subs before benchmarking

foreach my $sub (\sub1, \sub2, \sub3, \sub4) {

foreach (1e7, 0, 5, 6, 0.00, -4, abc, f4fc, '+.1', undef) {

my $val = defined $_ ? $_ : 'undef';
my $ret = $sub ($_);
print $ret ? sub1 ($ret): '$val' is a number\n :
  sub1 ($ret): '$val' is not a number\n;
$ret = $sub ($_);
print $ret ? sub2 ($ret): '$val' is a number\n :
  sub2 ($ret): '$val' is not a number\n;
$ret = $sub ($_);
print $ret ? sub3 ($ret): '$val' is a number\n :
  sub3 ($ret): '$val' is not a number\n;
$ret = $sub ($_);
print $ret ? sub4 ($ret): '$val' is a number\n :
  sub4 ($ret): '$val' is not a number\n;
}
}

no warnings;# needed for Chris solution
no strict;  # needed for Chris solution

my $count = 100;
cmpthese ($count, {
RE = 'main (\sub1)',
IF = 'main (\sub2)',
STRTOD = 'main (\sub3)',
LLN = 'main (\sub4)',
  });

sub main {
my $cref = shift;

foreach (1e7, 0, 5, 6, 0.00, -4, abc, f4fc, '+.1', undef) {
$cref ($_);
}

}

sub sub1 {
my $var = shift;

$var =~ s/^\s+//; $var =~ s/\s+$//;
return 0 if not defined $var or $var eq '';

if ($var eq $var + 0) {
return 1;
} elsif (eval $var eq $var + 0) {
return 2;
}
return 0;

}

sub sub2 {
my $var = shift;

$var =~ s/^\s+//; $var =~ s/\s+$//;
return 0 if not defined $var or $var eq '';

return 1 if $var =~ /^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/;
return 0;

}

sub sub3 {
my $var = shift;

$var =~ s/^\s+//; $var =~ s/\s+$//;
return 0 if not defined $var or $var eq '';

$! = 0;
my ($num, $unparsed) = strtod ($var);
return 0 if $unparsed or $!;
return 1;

}

sub sub4 {
my $var = shift;

$var =~ s/^\s+//; $var =~ s/\s+$//;
return 0 if not defined $var or $var eq '';

# return looks_like_number ($var);
# code stripped from LLN:

return 1 if (/^[+-]?\d+$/); # is a +/- integer
return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float
return 1 if ($] = 5.008 and /^(Inf(inity)?|NaN)$/i) or
  ($] = 5.006001 and /^Inf$/i);

}

__END__


-- 
  ,-/-  __  _  _ $Bill LuebkertMailto:[EMAIL PROTECTED]
 (_/   /  )// //   DBE CollectiblesMailto:[EMAIL PROTECTED]
  / ) /--  o // //  Castle of Medieval Myth  Magic http://www.todbe.com/
-/-' /___/__/_/_http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: Test if string is a number?

2005-06-30 Thread Chris Wagner
At 02:28 PM 6/30/05 -0500, Joe Discenza wrote:
Regex is pretty fast. Eval is usually pretty slow.

Yeah ur right about the eval.  I did a triple head to head with ur regex and
eval/no eval.  The eq without the eval demolishes all.

  Rate   evalRE noeval
eval3397/s --  -87%   -96% eval $var eq $var + 0;
RE 25253/s   643%--   -72% /^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/
noeval 90909/s  2576%  260% -- $var eq $var + 0;






--
REMEMBER THE WORLD TRADE CENTER ---= WTC 911 =--
...ne cede males

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: Test if string is a number?

2005-06-29 Thread Lyle Kopnicky
Thanks folks.  I think I'll go with looks_like_number from 
Scalar::Util.  I like to use library routines where possible.  I don't 
know how I overlooked that, since I poked through Scalar::Util earlier.


It just seems bizarre to me that something like that isn't a builtin.  I 
mean, you can't even tell strings from numbers?  I'm used to working in 
strongly typed languages.


--
Lyle Kopnicky
Software Project Engineer
Veicon Technology, Inc.

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs