zoukankan      html  css  js  c++  java
  • 【perl】perl实现case条件判断的语句

    方法一: 5.8版本 使用Switch包

     use Switch;
            switch ($val) {
                case 1          { print "number 1" }
                case "a"        { print "string a" }
                case [1..10,42] { print "number in list" }
                case (@array)   { print "number in list" }
                case /\w+/      { print "pattern" }
                case qr/\w+/    { print "pattern" }
                case (%hash)    { print "entry in hash" }
                case (\%hash)   { print "entry in hash" }
                case (\&sub)    { print "arg to subroutine" }
                else            { print "previous case not true" }
            }

    方法二:5.10版本

    use 5.010;
    given( $ARGV[0] ) {
        when( /fred/i ) { say 'Name has fred in it' }
        when( /^Fred/ ) { say 'Name starts with Fred' }
        when( 'Fred'  ) { say 'Name is Fred' }
        default         { say "I don't see a Fred" }
        }

     方法三:用hash做switch 和case (个人觉得最有趣的方法)

    use strict;
    use warnings;
    my %subhash;
    $subhash{a}=&a;
    $subhash{b}=&b;
    $subhash{c}=&c;
    my $case = <STDIN>;
    chomp $case;
    &{$subhash{$case}};
    sub a{print "a\n";}
    sub b{print "b\n";}
    sub c {print "c\n";}

  • 相关阅读:
    mysql数据库存储的引擎和数据类型
    mysql数据库基本操作
    【转】linux yum命令详解
    [转]Linux rpm 命令参数使用详解
    【转】Linux GCC常用命令
    [转]linux下logrotate 配置和理解
    [转]Linux下chkconfig命令详解
    [转]linux之top命令
    [转]linux之ps命令
    互联网产品如何做到快与轻
  • 原文地址:https://www.cnblogs.com/xianghang123/p/2355425.html
Copyright © 2011-2022 走看看