zoukankan      html  css  js  c++  java
  • Perl 数字与字符串运算符之区别

    在Perl中,字符串比较和数字比较是由不同的运算符来完成的:

    • 数字比较运算符: <, >, <=, >=, ==, !=
    • 字符串比较运算符: lt, gt, le, ge, eq, ne
    • cmp: 比较字符串,返回 -1, 0 或者 1。
    • <=>: 比较数字,返回 -1, 0 或者 1。
    • =~:用正则表达式来匹配,匹配的话返回True。
    • !~:和 =~ 相反,不匹配返回True。
    • 例子

    #!/usr/bin/env perl  

    use strict;  

    use warnings;  

       

    my $num1 = 1;  

    my $num2 = 1.0;  

    my $two_numbers = "$num1 and $num2";  

       

    my $str1 = "1abc";  

    my $str2 = "1xyz";  

    my $two_strings = "$str1 and $str2";  

       

    print "Compare two numbers using != ";  

    if ($num1 != $num2) {  

        print "$two_numbers are not equal ";  

    } else {  

        print "$two_numbers are equal ";  

    }  

       

    print "Compare two strings using !=: get wrong result ";  

    if ($str1 != $str2) {  

        print "$two_strings are not equal ";  

    } else {  

        print "$two_strings are equal ";  

    }  

       

    print "Compare two numbers using ne ";  

    if ($num1 ne $num2) {  

        print "$two_numbers are not equal ";  

    } else {  

        print "$two_numbers are equal ";  

    }  

       

    print "Compare two strings using ne ";  

    if ($str1 ne $str2) {  

        print "$two_strings are not equal ";  

    } else {  

        print "$two_strings are equal ";  


     

    结果:

    Compare two numbers using !=  

    1 and 1 are equal  

    Compare two strings using !=: get wrong result  

    Argument "1xyz" isn't numeric in numeric ne (!=) at compare_operators.pl line 23.  

    Argument "1abc" isn't numeric in numeric ne (!=) at compare_operators.pl line 23.  

    1abc and 1xyz are equal  

    Compare two numbers using ne  

    1 and 1 are equal  

    Compare two strings using ne  

    1abc and 1xyz are not equal 

  • 相关阅读:
    NPOI操作Excel(三)--解析Excel
    NPOI操作Excel(二)--创建Excel并设置样式
    NPOI操作Excel(一)--NPOI基础
    git使用
    10、生鲜电商平台-财务系统模块的设计与架构
    9、生鲜电商平台-推荐系统模块的设计与架构
    session详解
    Java中的Synchronized关键字用法
    Java线程安全与多线程开发
    8、生鲜电商平台-购物车模块的设计与架构
  • 原文地址:https://www.cnblogs.com/xiaopengren/p/3431211.html
Copyright © 2011-2022 走看看