объясните в цикле PERL в цикле?

Это код для поиска слов, символов, строк в файле. Может кто-нибудь объяснить цикл while?

open(FILE, "<data.txt") or die "Could not open file: $!";

my ($lines, $words, $chars) = (0,0,0);

while (<FILE>) {
    $lines++;
    $chars += length($_); //what _ stands for?
    $words += scalar(split(/\s+/, $_)); //what is /\s+/, $_
}

print("lines=$lines words=$words chars=$chars\n");
1
задан 19 October 2015 в 16:12

1 ответ

Предполагая, что это Perl (вы должны были сделать это ясно).

Прочитайте man perlintro, в котором говорится, в частности:

Files and I/O
   You can open a file for input or output using the "open()" function.
   It's documented in extravagant detail in perlfunc and perlopentut, but
   in short:

    open(my $in,  "<",  "input.txt")  or die "Can't open input.txt: $!";
    open(my $out, ">",  "output.txt") or die "Can't open output.txt: $!";
    open(my $log, ">>", "my.log")     or die "Can't open my.log: $!";

   You can read from an open filehandle using the "<>" operator.  In
   scalar context it reads a single line from the filehandle, and in list
   context it reads the whole file in, assigning each line to an element
   of the list:

    my $line  = <$in>;
    my @lines = <$in>;

Если этого недостаточно, прочитайте man perlfunc и man perlopentut

0
ответ дан 23 May 2018 в 16:34

Другие вопросы по тегам:

Похожие вопросы: