zoukankan      html  css  js  c++  java
  • 练习册第17章课后习题

    17.1. Write a program that repeatedly asks the user for a string and a regular expression
    then reports if the string matches the regular expression. What happens if you give your
    program an invalid regular expression? How can you fix that? If you can’t think of an
    invalid regular expression, try one with an unmatched

    use 5.016;
    use autodie;
    
    {
        print "Enter a string:";
        chomp(my $string = <STDIN>);
        last unless defined $string;
    
        print "Enter a regex:";
        chomp(my $regex =<STDIN>);
    
        eval {
            if ($string =~ /$regex/) {
                say 'The string matches!';
            } else {
                say 'The string does not match.';
            }
    
    
        };
    
        say "Could not use regular expression:$@" if $@;
    
        redo;
    
    }

    17.2. Rewrite your answer to Exercise 17.1 to use Try::Tiny instead of eval (or, if you
    used Try::Tiny in the previous exercise, try it with eval).

    use 5.016;
    use autodie;
    use Try::Tiny;
    
    {
        print "Enter a string:";
        chomp(my $string = <STDIN>);
        last unless defined $string;
    
        print "Enter a regex:";
        chomp(my $regex =<STDIN>);
    
        try {
            if ($string =~ /$regex/) {
                say 'The string matches!';
            } else {
                say 'The string does not match.';
            }
    
        }catch {
            say "Could not use regular expression:$_" 
    
        }; 
    
        
    
        redo;
    
    }

    17.3. Use the grep function to select the multiples of 3 between 1 and 1,000.

    use 5.016;
    use autodie;
    my @three = grep { not $_ % 3 } 1 .. 1000;

    17.4. Use the map function to create a list of the squares of the numbers from 1 to 10.

    use 5.016;
    use autodie;
    
    my @squares = map { $_ **2 } (1..10);

    17.5. Given this single string (you have to store the entire thing in one scalar even
    though it looks like multiple lines), transform it so that each of the names at the beginning
    of each line have an initial capital letter, and the rest of the name is in lowercase
    letters:
    fRED has score 230
    barney has score 190
    DINO has score 30
    Your program should report this output:
    Fred has score 230
    Barney has score 190
    Dino has score 30

    use 5.016;
    use autodie;
    
    my $string = "fRed has score 230
    barney has score 190
    DINO has score 30";
    
    
    $string =~ s/^(S+)/uL$1/mg;
    
    print $string;

    17.6. Given an array whose elements are the numbers from 0 to 1,000, use an array
    slice to a) select the first and last numbers (in that order), b) the last and first numbers
    (in that order), c) the first ten numbers, and d) the odd numbers.

    use 5.016;
    use autodie;
    
    my @numbers = 1 .. 1000;
    
    my ($first,$last) = @numbers[0,-1];
    say "first is $first and last is $last
    ";
    
    ($last,$first) = @numbers[-1,0];
    say "first is $first and last is $last
    ";
    
    my @first_ten   = @numbers[ 0 .. 9 ];
    say "The first ten numbers are @first_ten";
    
    my @odd_numbers = @numbers[ grep {  $_ % 2 } 0 .. $#numbers ] ;
    say "The odd numbers are @odd_numbers
    ";

    17.7. Given the start of a program that looks up the characters’ bowling scores, add a
    line to create the hash using a hash slice. The names are the keys, and the values are
    their scores. Once you’ve done that, change the program to create the hash using a
    map.

    use 5.016;
    use autodie;
    
    my @names = qw(Fred Barney Dino);
    my @scores = qw(230 190 30 );
    # CREATE THE HASH HERE...
    my %scores = ();
    
    #@scores{@names} = @scores;
    
    %scores = map {($names[$_],$scores[$_])} 0 .. $#names;
    
    foreach my $name ( sort keys %scores ) {
        print "$name has score $scores{$name}
    ";
    }

    17.8. Add up the numbers from 1 to 1,000. Do it using the sum subroutine from
    List::Util, then redo it by using reduce from the same module.

    use 5.016;
    use autodie;
    use List::Util qw(sum reduce);
    
    say 'The sum of 1 to 1000 is ',sum( 1 .. 1000 );
    
    say 'The sum of 1 to 1000 is ', reduce { $a + $b } ( 1 .. 1000 );

    17.9. Use the pairwise subroutine from List::MoreUtils to add the corresponding
    elements in two arrays. For example, you have these two arrays:
    my @m = ( 1, 2, 3 );
    my @n = ( 4, 6, 8 );
    You should create a third array with the sums of the two first elements, the two second
    elements, and so on:
    my @o = ( 5, 8, 11 );

    use 5.016;
    use autodie;
    use List::MoreUtils qw(pairwise);
    
    my @m = ( 1, 2, 3 );
    my @n = ( 4, 6, 8 );
    
    my @o = pairwise {$a+$b} @m,@n;
    
    say "The sums are (@o)";
  • 相关阅读:
    ArrayList排序Sort()方法(转)
    sqlserver2008 insert语句性能
    Installing TensorFlow on Ubuntu
    自动下载和安装 MNIST 到 TensorFlow 的 python 源码 (转)
    c# BackGroundWorker 多线程操作的小例子 (转)
    c# 修改winform中app.config的配置值
    ffmpeg 编译graph2dot
    ffmpeg常用命令
    live555例子程序编译连接时发现函数未定义问题
    编译代码是提示某些类型错误问题
  • 原文地址:https://www.cnblogs.com/tjxwg/p/3367908.html
Copyright © 2011-2022 走看看