convert string to number?

Hi,
I have a problem where a number read from a file is being treated as a
string by perl (I think!). I am using the module RSPerl which is an
interface between Perl and the statistical language R. When i read a
column of numbers from a file and pass it to the perl/R function i get
an "invalid 'type' (character) of argument" error.
The reason i think this is a problem on the Perl side is shown by this
pseudocode:
while(<FILE>){
   ... stuff to extract $value from each row ...
   push(@list1, $value);
}
@list2 = (100.2, 232.333, 344.2);   # these are the numbers from file
my_R_func_call(\@list1);
my_R_func_call(\@list2);
using @list1 gives the error, @list2 does not. if i add a line that
performs a redundant mathematical operation on $value before push'ing it
into @list1 eg
   if($value < 1000000000000){}
   push(@list1, $value);
then the error goes away, therefore i suspect this is a problem with the
internal datatype. I have never worried about this in Perl before, but
it appears to be more important now as R is more strict.
is there a way in Perl to force a variable to be a number rather than a
string?
thanks
adam
-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
0
awitney
7/7/2006 8:18:03 AM
📁 perl.macosx
📃 2309 articles.
⭐ 0 followers.

💬 3 Replies
👁️‍🗨️ 855 Views


Hi Adam,
sounds like the symptoms you'd get if the value you're sending to R  
has a newline character hanging on the end. Perl will do the  
conversion to an integer when required [1], but if you're passing  
such a thing to R it'll probably get grumpy. Just chomp the line  
before doing the extraction of the values and with any luck all will  
be well.
[1] For example...
#!/usr/bin/perl
use warnings;
use strict;
my $string="98\n";
my $sub=$string-10;
print "OK we now have $sub\n";
=====>>>> OK we now have 88
Cheers,
Paul
0
paul
7/7/2006 12:20:27 PM
On 2006=9607=9607, at 10:18, Adam Witney wrote:
> is there a way in Perl to force a variable to be a number rather =20
> than a
> string?
Yes. You already discovered it: perform a redundant mathematical =20
operation on it. The usual idiom is to add zero to it. (Conversely, =20
to force something to be a string, append an empty string to it.)
What's happening internally is that (among other things), perl =20
variables can accommodate for both string and numeric representations =20=
of the variable's value, but they're filled in lazily: a string is =20
not converted to a number until the variable is used in a numeric =20
context (and vice versa). Despite this laziness, Perl is usually able =20=
to do what you mean as far as numbers and strings are concerned, but =20
sometimes you have to give it a hint. For more information, see the =20
perlnumber man page.
--=20
Dominic Dunlop
0
shouldbedomo
7/7/2006 12:26:54 PM
On Jul 7, 2006, at 3:18 AM, Adam Witney wrote:
> then the error goes away, therefore i suspect this is a problem  
> with the
> internal datatype. I have never worried about this in Perl before, but
> it appears to be more important now as R is more strict.
I'd call this a bug in the RSPerl code, actually.  If it's looking  
for a number, it should use the SvNV() or SvIV() macros (to get a  
double or integer, respectively).  I suspect it's using SvIOK() or  
SvNOK(), which don't attempt to do any automatic coercion.  This  
forces you, the caller, to do it.
Check out the first couple pages of `perldoc perlguts` for the details.
  -Ken
0
ken
7/11/2006 4:23:45 AM
Reply: