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

Signature
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Paul McCann - 07 Jul 2006 13:20 GMT
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
Dominic Dunlop - 07 Jul 2006 13:26 GMT
> is there a way in Perl to force a variable to be a number rather
> than a
> string?
Yes. You already discovered it: perform a redundant mathematical
operation on it. The usual idiom is to add zero to it. (Conversely,
to force something to be a string, append an empty string to it.)
What's happening internally is that (among other things), perl
variables can accommodate for both string and numeric representations
of the variable's value, but they're filled in lazily: a string is
not converted to a number until the variable is used in a numeric
context (and vice versa). Despite this laziness, Perl is usually able
to do what you mean as far as numbers and strings are concerned, but
sometimes you have to give it a hint. For more information, see the
perlnumber man page.

Signature
Dominic Dunlop
Ken Williams - 11 Jul 2006 05:23 GMT
> 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