• 练习册第15章习题


    15.1. [Perl 5.10.1] Use the smart match operator in a program that prompts the user
    for a string and reports if that string is an element of an array you specify in your
    program. You only have to match exact elements.

    use 5.016;
    my @array = qw(Fred Wilma Barney Betty Larry Pebbles Bamm-Bamm);
    say "The elements are (@array)";
    say "Enter a name:";
    
    chomp(my $name=<STDIN>);
    
    given($name) {
    
        when ($name ~~ @array) {say "I found a matching name"}
        default {say "I didn't find a matching name"}
    
    }

    15.2. [Perl 5.10.1] Modify your answer to Exercise 15.1 to prompt the user to enter
    several names, one line per name. Report success if at least one of the names is a key
    in a hash that you define in your program.

    use 5.016;
    my %hash = qw(
    Fred Flintstone
    Wilma Flintstone
    Barney Rubble
    Betty Rubble
    Larry Slate
    Pebbles Flintstone
    Bamm-Bamm Rubble
    );
    
    say "The keys:[",(join ' ',keys %hash),"]";
    say "Enter some names then Control-Z to stop:";
    
    chomp (my @inputs=<STDIN>) ;
    
    if (@inputs ~~ %hash) {
        say 'I found a matching name';
    }else {
        say "I didn't find a matching name";
    }

    15.3. [Perl 5.10.1] Write a program that prompts the user for a regular expression
    pattern and reports if any elements of an array match that pattern. Use the smart match
    operator to check if that pattern matches. You don’t need to report which elements
    match. As a bonus, what do you do if the user enters an invalid regular expression

    use 5.016;
    my @array = qw(Fred Wilma Barney Betty Larry Pebbles Bamm-Bamm);
    say "The elements are (@array)";
    say "Enter a pattern:";
    chomp(my $reg=<STDIN>);
    
    if ( eval{ /$reg/ ~~ @array} ) {
        say "At least one element matches";
    }else {
        say "No element matches";
    }
    
    if ($@) {
        say "error:$@";
    }

    15.4. [Perl 5.10.1] Write a program that prompts you for two lists of words. Use the
    smart match operator to report if these two lists are the same or not. To make things
    easier, enter each list on a single line then split that line on whitespace.

    use 5.016;
    say "Enter the first list on a single line:";
    my @first = split /s+/,<STDIN> ;
    
    
    say "Enter the second list on a single line:";
    my @second = split /s+/,<STDIN> ;
    
    if (@first ~~ @second) {
        say 'The lists are the same'; 
    }else {
        say 'The lists are not the same';
    }

    15.5. [Perl 5.10.1] Use given-when and smart matching to implement a word guessing
    game. If the user enters a string that exactly matches the secret word, report success.
    If the string doesn’t match, try that string as a regular expression pattern. If the user
    enters give up, stop the program (and, if you want to be nice, tell them the secret word).

    #!/usr/bin/perl
    # 第15章 练习册 课后习题5
    use 5.016;
    
    my $Verbose = $ENV{VERBOSE} // 1 ;
    my $secret = "david";
    
    print 'Enter a string or pattern>';
    
    while (1) {
        chomp(my $guess=<STDIN>);
        given($guess){
            when('give up') {
                say 'Too hard? Better luck next time!';
                last ;
            }
    
            when($secret){
                say 'You gueesed the secret word!';
                last;
    
    
            }
    
            when( $secret ~~ /$_/){
                say "The secret word matcheds the pattern [$guess]";
                print '
    Enter another string or pattern>';
    
            }
    
            default {
                say "[$guess] did not help at all!";
                print '
    Enter another string or pattern>';
    
            }
    
    
            
    
    
        }
    }
  • 相关阅读:
    [转载]SELinux 环境下网络服务设置,配置 Apache 、Samba、NFS
    [转载]SELinux安全系统基础
    Linux NFS服务器的安装与配置
    将可执行程序设为linux服务启动
    不关闭selinux下配置php+httpd访问KingbaseES
    [转载]SELinux安全系统基础
    解决samba和SELINUX 冲突
    [转载]在线文本编辑器的基本实现原理
    [转载]使用postgresql安装wordpress
    [转载]php连接postgreSQL数据库及其操作(php5,postgreSQL9)
  • 原文地址:https://www.cnblogs.com/tjxwg/p/3358739.html
Copyright © 2020-2023  润新知