zoukankan      html  css  js  c++  java
  • 练习册第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>';
    
            }
    
    
            
    
    
        }
    }
  • 相关阅读:
    Qt 资料大全
    Node.js+websocket+mongodb实现即时聊天室
    win系统下nodejs安装及环境配置
    nginx关于 error_page指令详解.md
    移动前端开发之viewport,devicePixelRatio的深入理解
    Iphone各个型号机型的详细参数,尺寸和dpr以及像素
    H5前端的关于像素解释
    从Pc转向H5开发遇到的适配问题思考
    Safari无痕模式下,storage被禁用问题
    滴滴开源 Vue 组件库— cube-ui
  • 原文地址:https://www.cnblogs.com/tjxwg/p/3358739.html
Copyright © 2011-2022 走看看