1

I would like to read a file in perl, after, the user can input any string and grep will try to find the string inputted in the file read. It will only exit when the user input nothing or any space character. Here's my code which is not working:

#! usr/bin/perl
use warnings;
use strict;

open MATCHSTRING,"matchstring";
my @lines = <MATCHSTRING>;

while (<>) {
    chomp;
    my @match = grep {/\b$_\b/s} @lines;
    print @match;
    }

I'm still lacking the condition where it will exit once nothing is inputted or a newline or any space character.

4

2 に答える 2

3
while (<>)

意味

while (defined($_ = <>))

そのため、Ctrl-D (unix) または Ctrl-Z、Enter (Windows) を押して、入力の終わりを知らせる必要があります。または、空白行のチェックを追加できます。

while (<>) {
   chomp;
   last if $_ eq "";
   print grep /\b$_\b/s, @lines;
}
于 2013-05-20T03:59:12.750 に答える