[comp.lang.perl] Converting a string to a number

pablo@mrspoc.Transact.COM (Pablo Sanchez) (07/14/90)

Hi there,

	I'm on a XENIX 386 (2.3.2) running Perl 3.0 pl 18.  My code is doing
something very simple.  I'm reading in a text line that has a number assigned
to it (e.g. "ROW_ORIGIN 8") and I need to get the number portion out.  Once I
get the number portion out, I wish to convert this to a number.  I thought I
could do it one way, but have had to work around it...

Here's the first way I thought I could do it:

	$last_screen_line[0] =+ 0 ;		# convert to a number

But on my system, this always prints out zero.  So instead I had to do the
following:

	$last_screen_line[0] = $last_screen_line[0] + 0 ;	

Has this been reported already (or is it a feature :-) )?
-- 

Pablo Sanchez               Tomic Drop (tom' ik drop) n. "Factory testing" of a
Transact Software, Inc      cat to assure oneself the "always lands on its
pablo@transact.com          feet" principle still holds true.

merlyn@iwarp.intel.com (Randal Schwartz) (07/14/90)

In article <PABLO.90Jul13100153@mrspoc.Transact.COM>, pablo@mrspoc (Pablo Sanchez) writes:
| 	I'm on a XENIX 386 (2.3.2) running Perl 3.0 pl 18.  My code is doing
| something very simple.  I'm reading in a text line that has a number assigned
| to it (e.g. "ROW_ORIGIN 8") and I need to get the number portion out.  Once I
| get the number portion out, I wish to convert this to a number.  I thought I
| could do it one way, but have had to work around it...
| 
| Here's the first way I thought I could do it:
| 
| 	$last_screen_line[0] =+ 0 ;		# convert to a number

Well, first, "=+" is not an operator.  But += doesn't work either, because
the string's value is 0.  Extract the number instead with a substitution.

$last_screen_line[0] = "ROW_ORIGIN 8";

$last_screen_line[0] =~ s/^\D+//; # eliminate all leading non-digits.
# $last_screen_line[0] =~ s/^\D*(\d+).*//; # if imbedded instead

print "value is $last_screen_line[0], and 3+that is ", 3+$last_screen_line[0];


| 
| But on my system, this always prints out zero.  So instead I had to do the
| following:
| 
| 	$last_screen_line[0] = $last_screen_line[0] + 0 ;	

This seems like it would always give zero... if I read the spec
properly.

print "Just another non-zero Perl hacker,"
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/