0

我在这方面完全是新手,但我很感激任何和所有的帮助。我正在运行此脚本以在过时的子目录中查找与特定序列匹配的字符串,然后将结果打印到新文件中。子目录被命名为“20110101”、“20110102”、“20110103”等。它可以很好地找到字符串,但在将它们打印到新文件时会将它们弄乱。它会在 20110107 之前打印 20110129。我该如何解决这个问题,以便它按字符串在子目录中的顺序打印字符串?在此先感谢您的帮助。

use strict;
use warnings;
use File::Find;

my $starting_path = "/d2/aschwa/test";

open my $output, '>>', 'KDUX_METARS.txt' or die $!; #open file to write data

print STDOUT "Finding METAR files\n";

find(

    sub {
        return unless -e -f;
        if ( open my $infile, '<', $_ ) {
            while ( my $line = <$infile> ) {
                print $output $line if $line =~ m/(KDUX ....05Z|KDUX ....55Z)/;
           }
        }
        else {
            warn "$_ couldn't be opened: $!";
        }
    },
    $starting_path
);
close $output or die $!;
4

1 回答 1

3
perl -E 'use File::Find; find({wanted => sub {say $File::Find::name}, preprocess => sub { reverse sort @_} }, ".")'
.
./20110129
./20110107
./20110101

perl -E 'use File::Find; find({wanted => sub {say $File::Find::name}, preprocess => sub { sort @_} }, ".")'
.
./20110101
./20110107
./20110129

您可以调用 File::Find::find 函数,例如 find(\%opts, @dirs);

$opts{wanted} == 对每个元素执行的操作(当调用 find(\&sub, @dirs) 时相当于 &sub )

$opts{preprocess} == 对整个列表的操作

于 2013-10-25T20:55:41.960 回答