views:

19065

answers:

9

How would I convert a string holding a number into an integer in Perl?

+32  A: 

You don't need to convert it at all:

% perl -e 'print "5.45" + 0.1;'
5.55
Alnitak
+2  A: 

Perl really only has three types: scalars, arrays, and hashes. And even that distinction is arguable. ;) The way each variable is treated depends on what you do with it:

perl -e "print 5.4 . 3.4;"

5.43.4

perl -e "print '5.4' + '3.4';"

8.8

Rini
Perl has many more types than, but for single values, it's just a single value.
brian d foy
you can also add 0
Nathan Fellman
+14  A: 

Perl is a context-based language. It doesn't do its work according to the data you give it. Instead, it figures out how to treat the data based on the operators you use and the context in which you use them. If you do numbers sorts of things, you get numbers:

# numeric addition with strings
my $sum = '5.45' + '0.01'; # 5.46

If you do strings sorts of things, you get strings:

# string replication with numbers
my $string = ( 44/2 ) x 3; # "22.522.522.5"

Perl mostly figures out what to do and it's mostly right. Another way of saying the same thing is that Perl cares more about the verbs than it does the nouns.

Are you trying to do something and it isn't working?

brian d foy
+1  A: 

Take what I say with a grain of salt, but it seems like you sometimes DO need to convert it, for example, when producing JSON output with floats in it.

I never figured out the right way to do it, so I just multiplied my string by one: 1 * $str

+2  A: 

I think the author wants a way to ensure that a value is converted to a numeric value. For example, I have the following code that converts to integer values: $val = int($val || 0); This will convert "25" to "25, "000025" to "25", blank space to 0, and the bogus character string "Bozo" to 0. However, this will only handle integers. Is there a way to do the same thing only with floats instead of integers? This way, the string "0000000.25" will convert to .25 and "Hello Dolly0.0" will convert to 0.0?

This is useful, for example, if you're reading spreadsheet cells into a database. If you're inputting into an oracle 'Number' field, you want all invalid and blank values to be input as 0.

Bob Thompson
The author has accepted an answer already. So it is fairly obvious that it answered the question.
Brad Gilbert
A: 

Um, correct me if I'm wrong, but calling sort on an array of floats will produce a different ordering than if those numbers are all converted to strings before being sorted. Thus, Perl's automatic handling is not always enough. I know there's an int() function, but there doesn't seem to be a float() function. What is it called?

phil
+1  A: 

Google lead me here while searching on the same question phill asked (sorting floats) so I figured it would be worth posting the answer despite the thread being kind of old. I'm new to perl and am still getting my head wrapped around it but brian d foy's statement "Perl cares more about the verbs than it does the nouns." above really hits the nail on the head. You don't need to convert the strings to floats before applying the sort. You need to tell the sort to sort the values as numbers and not strings. i.e.

my @foo = ('1.2', '3.4', '2.1', '4.6');
my @foo_sort = sort {$a <=> $b} @foo;

See http://perldoc.perl.org/functions/sort.html for more details on sort

Norm
A: 

As I understand it int() http://perldoc.perl.org/functions/int.html is not intended as a 'cast' function for designating data type it's simply being (ab)used here to define the context as an arithmetic one. I've (ab)used (0+$val) in the past to ensure that $val is treated as a number.

mccutchm
A: 

I have this problem:

use strict;
use warnings;

my $var = '0x810E';
my $var2 = $var + 1;
print "$var + 1 = ", $var2;

the output is:

C:\perl>perl prova.pl
Argument "0x810E" isn't numeric in addition (+) at prova.pl line 5.
0x810E + 1 = 1
C:\aep\code\import\APM2ET>

How can I make calculations with hexadecimal integers when they are read as a string from an external file?

Ciao, Max

I find the 'hex' built in function... sorry!

Max
@Max, I think you'd better post this quesiton as a new one rather than a follow-up thred. BTW, I think if you change "my $var = '0x810E';" to "my $var = 0x810E;" without the single quotes, you might get the expected result.
Mike