share
Stack OverflowHow do I get my perl script to the found scripts in order?
[0] [1] WXresearcher
[2013-10-25 20:34:26]
[ perl search file-io printing ]
[ https://stackoverflow.com/questions/19598979/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 $!;
[+3] [2013-10-25 20:55:41] nyaapa
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


1