Re: numeric vs string scalars...difference?
Interesting. I checked the underlying code to Scalar::Util and it does a regex check, but also allows for exponential notation (as you eluded to). If my client's sales data has to be represented by exponential notation, I'm wayyy undercharging them! paul --- - Original Message - From: "Thomas, Mark - BLS CTR" <[EMAIL PROTECTED]> To: "'Paul Rogers'" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Wednesday, November 17, 2004 2:07 PM Subject: RE: numeric vs string scalars...difference? How about 1.02e12? If you want to know if it looks like a number to perl, this might be handy: use Scalar::Util qw(looks_like_number); print "Number" if looks_like_number($number); Scalar::Util is part of the standard library in 5.8.x, I believe. Mark Thomas ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: numeric vs string scalars...difference?
How about 1.02e12? If you want to know if it looks like a number to perl, this might be handy: use Scalar::Util qw(looks_like_number); print "Number" if looks_like_number($number); Scalar::Util is part of the standard library in 5.8.x, I believe. -- Mark Thomas Internet Systems Architect ___ BAE SYSTEMS Information Technology 2525 Network Place Herndon, VA 20171 USA > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On > Behalf Of Paul Rogers > Sent: Wednesday, November 17, 2004 1:29 PM > To: [EMAIL PROTECTED] > Subject: Re: numeric vs string scalars...difference? > > > Hi Paul, > > You are absolutely right. However, the real problem was that > I had to > dynamically determine whether to use == or eq...thus I needed > a way of > reliably determining whether a variable was numeric or > string. That's where > the regex came into play. > > Thanks, > Paul --- > > - Original Message - > From: "Paul Sobey" <[EMAIL PROTECTED]> > To: "Paul Rogers" <[EMAIL PROTECTED]> > Cc: <[EMAIL PROTECTED]> > Sent: Wednesday, November 17, 2004 4:16 AM > Subject: RE: numeric vs string scalars...difference? > > > Hi, > > Isn't eq a string comparison? Try using == instead: > > print "String compares\n" if "45" eq "45.0"; # Fails > print "Number compares\n" if "45" == "45.0"; # Passes > > Cheers, > Paul > > > > ___ > Perl-Win32-Users mailing list > [EMAIL PROTECTED] > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs > ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: numeric vs string scalars...difference?
Hi Paul, You are absolutely right. However, the real problem was that I had to dynamically determine whether to use == or eq...thus I needed a way of reliably determining whether a variable was numeric or string. That's where the regex came into play. Thanks, Paul --- - Original Message - From: "Paul Sobey" <[EMAIL PROTECTED]> To: "Paul Rogers" <[EMAIL PROTECTED]> Cc: <[EMAIL PROTECTED]> Sent: Wednesday, November 17, 2004 4:16 AM Subject: RE: numeric vs string scalars...difference? Hi, Isn't eq a string comparison? Try using == instead: print "String compares\n" if "45" eq "45.0"; # Fails print "Number compares\n" if "45" == "45.0"; # Passes Cheers, Paul ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: numeric vs string scalars...difference?
Hi, Isn't eq a string comparison? Try using == instead: print "String compares\n" if "45" eq "45.0"; # Fails print "Number compares\n" if "45" == "45.0"; # Passes Cheers, Paul -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Paul Rogers Sent: 17 November 2004 02:44 To: Perl-Win32-Users Subject: Re: numeric vs string scalars...difference? Hi Andy, Thanks for your response. For clarification I'm doing equality comparisons. Thus... $x = '45.0'; $y = '45'; if ($x eq $y) {print "values are equal";} ...does not product the desired result (i.e., that they are indeed equal). I used your regex idea to solve the problem (I had some tried some non-regex techniques (namely using unpack) to determine numeric or string status but hit only deadends). I couldn't quite get your regex to work as required so I did some further sloothing and found "Programming Perl" code which more or less mimics the IsNumeric VB function: if ($mytestvar =~ /^-?(\d+\.?\d*|\.\d+)$/) { print "This is a numeric value."; } Thanks, Paul --- - Original Message - From: Andy Bach To: "Paul Rogers" <[EMAIL PROTECTED]> Sent: Tuesday, November 16, 2004 6:03 PM Subject: Re: numeric vs string scalars...difference? > "45.0" vs "45". > > So should they match (as numbers) or differ (as strings)? > > Assuming the former, you need to do some REs and it'll depend upon your > range of number-like strings. If a simple RE like: > foreach my $data1 ( @first_array ) { >foreach my $data2 ( @second_array ) { > if ( $data1 =~ /^\d*(?:\.\d+)$/ ) { # basic number >print "Matched $data1 == $data2\n" > if ( $data1 == $data2 ) >} elsif ( $data1 eq $data2 ) { >print "Matched $data1 eq $data2\n"; >} > }# foreach $data2 > } # foreach $data1 > > > That's a number as only digits and a possible '.' e.g. 1, 1.0, 0.1 and .1 > > If you don't want the "data2 is not a number" complaint > if ( $data1 =~ /^\d*(?:\.\d+)$/ and >$data2 =~ /^\d*(?:\.\d+)$/ ) { # basic number > > Untested but you get the idea. > > a > > Andy Bach, Sys. Mangler ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs * Gloucester Research Limited believes the information provided herein is reliable. While every care has been taken to ensure accuracy, the information is furnished to the recipients with no warranty as to the completeness and accuracy of its contents and on condition that any errors or omissions shall not be made the basis for any claim, demand or cause for action. * ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: numeric vs string scalars...difference?
Hi Andy, Thanks for your response. For clarification I'm doing equality comparisons. Thus... $x = '45.0'; $y = '45'; if ($x eq $y) {print "values are equal";} ...does not product the desired result (i.e., that they are indeed equal). I used your regex idea to solve the problem (I had some tried some non-regex techniques (namely using unpack) to determine numeric or string status but hit only deadends). I couldn't quite get your regex to work as required so I did some further sloothing and found "Programming Perl" code which more or less mimics the IsNumeric VB function: if ($mytestvar =~ /^-?(\d+\.?\d*|\.\d+)$/) { print "This is a numeric value."; } Thanks, Paul --- - Original Message - From: Andy Bach To: "Paul Rogers" <[EMAIL PROTECTED]> Sent: Tuesday, November 16, 2004 6:03 PM Subject: Re: numeric vs string scalars...difference? "45.0" vs "45". So should they match (as numbers) or differ (as strings)? Assuming the former, you need to do some REs and it'll depend upon your range of number-like strings. If a simple RE like: foreach my $data1 ( @first_array ) { foreach my $data2 ( @second_array ) { if ( $data1 =~ /^\d*(?:\.\d+)$/ ) { # basic number print "Matched $data1 == $data2\n" if ( $data1 == $data2 ) } elsif ( $data1 eq $data2 ) { print "Matched $data1 eq $data2\n"; } }# foreach $data2 } # foreach $data1 That's a number as only digits and a possible '.' e.g. 1, 1.0, 0.1 and .1 If you don't want the "data2 is not a number" complaint if ( $data1 =~ /^\d*(?:\.\d+)$/ and $data2 =~ /^\d*(?:\.\d+)$/ ) { # basic number Untested but you get the idea. a Andy Bach, Sys. Mangler ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: numeric vs string scalars...difference?
Paul Rogers [perl-users AT coservers DOTnet] wrote: > I need to dynamically do comparisons tests on an array of > values...some strings, some numeric. One snag I hit is > comparing the occasional numeric values. Using "eq" as > the comparison operator fails on comparisons like > "45.0" vs "45". > > Is there a way to determine if a scalar value is indeed numeric? Look in the Perl FAQ for various regex tests for a numeric. Then do this (pseudo-code): if ( is_numeric( $left) && is_numeric( $right ) ) { $is_equal = $left == $right; } else { $is_equal = $left eq $right; } That is, only use the numeric equality test '==' if both values are numeric. Otherwise use the string equality test 'eq'. -- Mike Arms ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs