How would I convert a string holding a number into an integer in Perl?
You don't need to convert it at all:
% perl -e 'print "5.45" + 0.1;'
5.55
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
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?
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.
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?
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
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.
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!