wstarrs has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to sort a list of filenames, then pull the first part of the name (a date YYYYMMDD) and compare with today's date and write that file into a new if the date in the name is on or before today, am I on the right track?
@sortedbus = sort {$b <=> $a} (@busdir); foreach $bus (@sortedbus) { @splbus = map {split (/_/, $bus, 1)} @sortedbus; if ($today >= @splbus[0]) { for ($i=0; $i++;) { $pers = @buschecked[i]; } # End for loop to populate the sorted, checked array } # End if statement to check the current date against the Begin d +ates } # End foreach loop to pull each name from the sorted array, check th +e begin date and populate the final array

Replies are listed 'Best First'.
Re: Could someone take a look at this code snippet?
by Masem (Monsignor) on May 10, 2001 at 20:31 UTC
    It might be easier to take out what you don't need with a grep:
    @to_be_written = grep { split(/_/)[0] <= $today } @busdir;
    ... after which, you just need to take each file in @to_be_written and do what you need to do with it.

    Alternative, it's probably more efficient to use a foreach loop, as to only process the list once for both the requirement on being less than $today, and for the file operations:

    foreach (@to_be_written) { if ( split(/_/)[0] <= $today ) { do_magic_file_copy_thing_here( $_ ); # $_ is the filename of int +erest } }

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Could someone take a look at this code snippet?
by stephen (Priest) on May 10, 2001 at 20:33 UTC
    At first glance, at least, it seems that your code is going to loop forever:
    # Loops forever for ($i=0; $i++;) { $pers = @buschecked[i]; }

    I'd suggest iterating over the list itself:

    # Goes through list for (@buschecked) { $pers = $_; }

    Plus, assigning to $pers doesn't actually do anything, so I'm not sure why you're doing it. :)

    May I suggest the File::Find module, plus Time::Local or one of the many other Time or Date modules on CPAN.

    Update: Changed Date::ParseDate to Time::Local, since it seemed more applicable.

    stephen

Re: Could someone take a look at this code snippet?
by thpfft (Chaplain) on May 10, 2001 at 20:41 UTC

    I confess to being confused by the use of map here and scared by the apparently endless loop, which probably means i haven't understood something. Here's how i would do the main bit, anyway:

    for (@sortedbus) { push (@oldbus,$_) if (m/^(\d)+?/ && make_nasty_date($1) < $today); }

    Which should give you a sorted list of the right files in @oldbus. make_nasty_date is standing for whatever mechanism you're using to convert yyyymmdd into seconds-since-epoch, assuming that's what you're doing.

    ps. i expect there are a hundred things wrong with the regex, so let's say it's assuming filenames in the right format.

Re: Could someone take a look at this code snippet?
by little (Curate) on May 11, 2001 at 16:08 UTC
    I feel you are mixing up arrays, slices and scalars a bit.
    This is a slice and not an array element: @buscheckedi
    However:
    foreach $bus (reverse sort @busdir) { $splbus = split (/_/, $bus, 1); if ($today >= $splbus) { $buschecked++; } }

    assuming I got what you wanted to do. But as it looks now it might then be easier to use the Schwartzian Transform.

    Have a nice day
    All decision is left to your taste