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([EMAIL PROTECTED]); my_R_func_call([EMAIL PROTECTED]); 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 1){} 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.
Re: convert string to number?
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
Re: convert string to number?
On 2006–07–07, at 10:18, Adam Witney wrote: 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. -- Dominic Dunlop
Updates for Mac-Carbon, Mac-Glue, etc.
Last night I released new Mac-Carbon, Mac-AppleEvents-Simple, and Mac-Glue updates, mostly for endian fixes for typeAbsoluteOrdinal values (such as gFirst, gAny) and typeLongDateTime values (which are quads). I also added a new function to Mac::Speech, to direct speech output to an AIFF file instead of the speakers, which I am using for my online radio show (podcast to some of you), where I generate questions from people using speech synthesis (http://pudge.net/ask/). -- Chris Nandor [EMAIL PROTECTED]http://pudge.net/ Open Source Technology Group [EMAIL PROTECTED] http://ostg.com/