2008-11-14 00:39
采纳率: 100%
浏览 1.4k

如何在 Perl 中将字符串转换为数字?

How would I convert a string holding a number into its numeric value in Perl?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

9条回答 默认 最新

  • local-host 2008-11-14 00:43

    You don't need to convert it at all:

    % perl -e 'print "5.45" + 0.1;'
    点赞 打赏 评论
  • 三生石@ 2008-11-14 01:25

    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;"
    % perl -e "print '5.4' + '3.4';"
    点赞 打赏 评论
  • 旧行李 2008-11-14 03:06

    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 = ( 45/2 ) x 4; # "22.522.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?

    点赞 打赏 评论
  • python小菜 2010-05-14 16:27

    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 for more details on sort

    点赞 打赏 评论
  • 七度&光 2010-08-25 17:22

    As I understand it int() 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.

    点赞 打赏 评论
  • 笑故挽风 2011-12-16 19:18

    This is a simple solution:

    Example 1

    my $var1 = "123abc";
    print $var1 + 0;



    Example 2

    my $var2 = "abc123";
    print $var2 + 0;


    点赞 打赏 评论
  • 10.24 2012-08-27 13:58

    In comparisons it makes a difference if a scalar is a number of a string. And it is not always decidable. I can report a case where perl retrieved a float in "scientific" notation and used that same a few lines below in a comparison:

    use strict;
    next unless $line =~ /and your result is:\s*(.*)/;
    my $val = $1;
    if ($val < 0.001) {
       print "this is small\n";

    And here $val was not interpreted as numeric for e.g. "2e-77" retrieved from $line. Adding 0 (or 0.0 for good ole C programmers) helped.

    点赞 打赏 评论
  • 乱世@小熊 2013-07-14 07:58
    $var += 0

    probably what you want. Be warned however, if $var is string could not be converted to numeric, you'll get the error, and $var will be reset to 0:

    my $var = 'abc123';
    print "var = $var\n";
    $var += 0;
    print "var = $var\n";


    var = abc123
    Argument "abc123" isn't numeric in addition (+) at line 7.
    var = 0
    点赞 打赏 评论
  • local-host 2013-07-30 15:47

    Perl is weakly typed and context based. Many scalars can be treated both as strings and numbers, depending on the operators you use. $a = 7*6; $b = 7x6; print "$a $b\n";
    You get 42 777777.

    There is a subtle difference, however. When you read numeric data from a text file into a data structure, and then view it with Data::Dumper, you'll notice that your numbers are quoted. Perl treats them internally as strings.
    Read:$my_hash{$1} = $2 if /(.+)=(.+)\n/;.
    Dump:'foo' => '42'

    If you want unquoted numbers in the dump:
    Read:$my_hash{$1} = $2+0 if /(.+)=(.+)\n/;.
    Dump:'foo' => 42

    After $2+0 Perl notices that you've treated $2 as a number, because you used a numeric operator.

    I noticed this whilst trying to compare two hashes with Data::Dumper.

    点赞 打赏 评论

相关推荐 更多相似问题