<data>阻止foreach循环被执行,为什么? :)</数据>

时间:2011-10-14 12:25:43

标签: 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); 
    }
} 

我得到了这个输出:

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

但是,如果我取消注释“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/

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

2 个答案:

答案 0 :(得分: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; 
    }
}

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

#!/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;
}

答案 1 :(得分:3)

Perl使用__DATA__在包的末尾创建伪数据文件。您可以使用文件句柄DATA来访问它,例如<DATA>。您的文件句柄是否可能存在冲突?尝试将文件句柄更改为其他内容,看看它是否更好。