<DATA>阻止foreach循环被执行,为什么?:)

Per*_*ika 1 perl foreach

我有两个嵌套的foreach循环.如果我使用此代码:

foreach (@directories) {
    my $actual_directory = $_;
    print "\nactual directory: ".$actual_directory."\n";

    foreach (@files) {
        my $file_name = $_; 
        my $actual_file = $actual_directory.$file_name;
        print $actual_file."\n";

        open(DATA, $actual_file) or die "Nelze otev?ít zdrojový soubor: $!\n";
        my $line_number = 0;

        #   while (<DATA>){
        #       my @znaky = split(' ',$_);
        #       my $poradi = $znaky[0]; #poradi nukleotidu
        #       my $hodnota = $znaky[1]; #hodnota

        #       my @temp = $files_to_sum_of_lines{$actual_file};
        #       $temp[$line_number] += $hodnota;    
        #       $files_to_sum_of_lines{$actual_file} = @temp;

        #       $line_number+=1;
        #   }
        #   close(DATA); 
    }
} 
Run Code Online (Sandbox Code Playgroud)

我得到了这个输出:

actual directory: /home/n/Plocha/counting_files/1/

/home/n/Plocha/counting_files/1/a.txt
/home/n/Plocha/counting_files/1/b.txt

actual directory: /home/n/Plocha/counting_files/2/

/home/n/Plocha/counting_files/2/a.txt
/home/n/Plocha/counting_files/2/b.txt
Run Code Online (Sandbox Code Playgroud)

但是,如果我取消注释" while (<DATA>){ }",我会松开a.txt和b.txt,所以输出如下所示:

actual directory: /home/n/Plocha/counting_files/1/

/home/n/Plocha/counting_files/1/a.txt
/home/n/Plocha/counting_files/1/b.txt

actual directory: /home/n/Plocha/counting_files/2/

/home/n/Plocha/counting_files/2/
/home/n/Plocha/counting_files/2/
Run Code Online (Sandbox Code Playgroud)

怎么能while (<DATA>)阻止我的foreach被执行?任何帮助将不胜感激.非常感谢.

Sin*_*nür 7

除了不使用之外DATA,请尝试使用词法循环变量和词法文件句柄.此外,Perl的内置功能$.可以为您跟踪行号.

for my $actual_directory (@directories) {
    print "\nactual directory: ".$actual_directory."\n";

    foreach my $file_name (@files) {
        my $actual_file = $actual_directory.$file_name;
        print $actual_file."\n";

        open my $INPUT, '<', $actual_file
            or die "Nelze otev?ít zdrojový soubor: $!\n";

        while (my $line = <$INPUT>) {
               my @znaky = split(' ', $line);
               my $poradi = $znaky[0]; #poradi nukleotidu
               my $hodnota = $znaky[1]; #hodnota
               @temp = $files_to_sum_of_lines{$actual_file};
               $temp[ $. ] += $hodnota;
               $files_to_sum_of_lines{$actual_file} = @temp;
        }
        close $INPUT; 
    }
}
Run Code Online (Sandbox Code Playgroud)

另一方面,我无法确定是否存在逻辑错误.以下内容可能有用:

#!/usr/bin/perl

use warnings; use strict;

use Carp;
use File::Find;
use File::Spec::Functions qw( catfile canonpath );

my %counts;
find(\&count_lines_in_files, @ARGV);

for my $dir (sort keys %counts) {
    print "$dir\n";
    my $dircounts = $counts{ $dir };
    for my $file (sort keys %{ $dircounts }) {
        printf "\t%s: %d\n", $file, $dircounts->{ $file };
    }
}

sub count_lines_in_files {
    my $file = canonpath $_;
    my $dir  = canonpath $File::Find::dir;
    my $path = canonpath $File::Find::name;

    return unless -f $path;

    $counts{ $dir }{ $file } = count_lines_in_file($path);
}

sub count_lines_in_file {
    my ($path) = @_;

    my $ret = open my $fh, '<', $path;

    unless ($ret) {
        carp "Cannot open '$path': $!";
        return;
    }
    1 while <$fh>;

    my $n_lines = $.;

    close $fh
        or croak "Cannot close '$path': $!";

    return $n_lines;
}
Run Code Online (Sandbox Code Playgroud)

  • @Sinan,拼写错误 - 我认为你的意思是`while <$ DATA>`引用你的词汇fh而不是内置的DATA句柄.在命名文件句柄时,也许最好避免使用任何"*what {DATA}":) (2认同)