简体   繁体   中英

How do I get my perl script to the found scripts in order?

I am a complete novice at this but I appreciate any and all help. I am running this script to find strings in dated subdirectories that match a particular sequence and then print the results to a new file. The subdirectories are named "20110101","20110102","20110103", etc. It finds the strings well but it takes them out of order when it prints them to the new file. It'll print 20110129 before 20110107. How can I fix this so that it prints the strings in the order that they are in in the subdirectories? Thanks in advance for the help.

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 $!;
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

You can call File::Find::find function like find(\\%opts, @dirs);

$opts{wanted} == action perfomed on every element (eq to to &sub when called find(\\&sub, @dirs) )

$opts{preprocess} == action on whole list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM