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

Hi,

i have 6 files in one folder which runs one after the other, but the real issue now is i need a specific file which should run 1st and the rest can run after this. now i am using this code

@filenames = sort { -M "$b" <=> -M "$a" } @filenames; foreach (@filenames){ push @file_list, "$_ \n"; } die if (!@file_list);

please help :)

2017-12-13 Athanasius added code and paragraph tags

Replies are listed 'Best First'.
Re: how to run a specific file from a list of filenames in a folder
by Corion (Patriarch) on Dec 12, 2017 at 10:15 UTC

    Which file is the first file, and how do you know that?

    Maybe you can show us examples of the files and the properties you use to determine which file is the first?

    sort is the correct approach to bringing an order to a list of items (like files). Your current code sorts the files by modification time (see -X). Maybe you want to sort by another criteria.

Re: how to run a specific file from a list of filenames in a folder
by Laurent_R (Canon) on Dec 12, 2017 at 11:19 UTC
    You are sorting the files of @filenames according to their last modification dates. And it seems to be doing that correctly (although you don't need to insert $a and $b between double quote marks, but that should still work correctly). Is this what you want? Please tell us in which respect your code does not satisfy your needs. Are you sure that the @filenames array is populated correctly before this part of your code starts to run? Perhaps you could print its content to check that.

    Also, please, use <code> and </code> tags for your code and data samples, in order to preserve their format.

Re: how to run a specific file from a list of filenames in a folder
by thanos1983 (Parson) on Dec 12, 2017 at 10:58 UTC

    Hello alenaustin,

    Welcome to the Monastery. A possible solution to your problem could be Thread::Queue.

    A "similar question" was asked in the past script should wait for another script to complete which contains several approaches in resolving the problem. On of those is:

    Maybe in your case there are not scripts to be executed but files to be processed, but the concept of the solution to the problem remains the same.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: how to run a specific file from a list of filenames in a folder
by kcott (Archbishop) on Dec 13, 2017 at 03:11 UTC

    G'day alenaustin,

    Welcome to the Monastery.

    I don't see the need for the foreach loop:

    $ > Y $ > X $ > Z $ perl -E 'my @f = qw{X Y Z}; say "@f"; @f = sort { -M $b <=> -M $a } +@f; say "@f"' X Y Z Y X Z

    Perhaps there are aspects of your issue that you haven't told us about. The guidelines in "How do I post a question effectively?" should help you to identify what else you need to tell us.

    — Ken

Re: how to run a specific file from a list of filenames in a folder
by Anonymous Monk on Dec 12, 2017 at 15:26 UTC
    To pull a particular item to the front, you can use something like this:
    my $first = 'run_this_first'; # or whatever @filenames = sort { ($b eq $first) cmp ($a eq $first) || -M $b <=> -M $a } @filenames;
    Or maybe something like this:
    my %modtimes; $modtimes{$_} = -M $_ foreach @filenames; $modtimes{$first} = 1e99; @filenames = sort { $modtimes{$b} <=> $modtimes{$a} } @filenames;
    Whatever floats your boat.