Perl Cd Bookshelf [Electronic resources] نسخه متنی

This is a Digital Library

With over 100,000 free electronic resource in Persian, Arabic and English

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید

A.16. Answer to Chapter 17 Exercises



  1. Here's one way to do it:

    my $filename = 'path/to/sample_text';
    open FILE, $filename
    or die "Can't open '$filename': $!";
    chomp(my @strings = <FILE>);
    while (1) {
    print "Please enter a pattern: ";
    chomp(my $pattern = <STDIN>);
    last if $pattern =~ /^\s*$/;
    my @matches = eval {
    grep /$pattern/, @strings;
    };
    if ($@) {
    print "Error: $@";
    } else {
    my $count = @matches;
    print "There were $count matching strings:\n",
    map "$_\n", @matches;
    }
    print "\n";
    }

    This one uses an eval block to trap any failure
    that might occur when using the regular expression. Inside that
    block, a grep pulls the matching strings from the
    list of strings.

    Once the eval is finished, we can report either
    the error message or the matching strings. Note that we
    "unchomped" the strings for output by using
    map to add a newline to each string .



/ 875