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

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: Sort files
by ikegami (Patriarch) on Apr 13, 2006 at 14:43 UTC

    No, at least not as desired.

    • There are two elements in each record, 0 and 1, yet you address 1 and 2.

    • One of the two elements is a string, yet you use the numerical compare (<=>) operator. Use the string one instead (cmp).

    • [stat]->[9] is needlessly slow. Use (stat)[9] instead.

    • You didn't specify how the string should be sorted, but I'm guessing you want to sort by mtime first, and by name last.

    If my guess is correct, you get:

    my @sorted_files_Test_flat_file_write_over_name_only = map { $_->[0] } sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] } map { [ $_, (stat)[9] ] } @files_Test_flat_file_write_over_name_only;
    or the faster
    my @sorted_files_Test_flat_file_write_over_name_only = map { substr($_, 11) } sort map { sprintf('%011d%s', (stat)[9], $_) } @files_Test_flat_file_write_over_name_only;

    Update: Formatting changes. Added code.

      or the fastest:
      use Sort::Key::Multi 'is_keysort'; # is_ => integer, string my @sorted = is_keysort { scalar((stat)[9]), $_ } @filenames;
      I am using (stat)[8] instead of (stat)[9] because that's what the OP probably wants.

      The scalar around (stat)[9] is required because stat could fail returning an empty list, and in list context, (()[9], $_) is equivalent to ($_). It is also required on ikegami GRT version.

      update: oops, stat[9] was ok, it seems I didn't count for the mtime position on stat return values properly, thanks ikegami for pointing it out.

Re: Sort files
by blue_cowdawg (Monsignor) on Apr 13, 2006 at 15:21 UTC
        Should this work?

    Totally off topic here, but that question reminds me of an event in my life when I was about 8 years old. I had become very interested in electronics at that age and my father (who has been an elecronics hobbyist for 60+ years) gave me a television set and an old radio to tear apart.

    Having torn them apart I surveyed the newley salvaged parts and decided to put some of them together and see what I could come up with. I had a power cord on one end and a bunch of parts hanging off of it and was all set to plug it in when I got a flash of wisdom and decided to run it by my dad before plugging it it.

    I dropped the whole mess on his lap and asked him "will this work?" His very dry response was: "Depends on what your intent was son... if you are looking to test the fuses in the house fuse box to see if they will blow or not it will work welll."

    So.. I guess the answer is: "It depends on what your intent was."

    Having said that I'm looking at your code and I think to myself "good grief!"

    I ran a modified version of it (only modifications were to provide some input to the array  @files_Test_flat_file_write_over_name_only (Why such a long variable names?) and to add a print Data::Dumper(@sorted_files_Test_flat_file_write_over_name_only) so I could see the results. Looks to my eye that all is happening is the files are being sorted by name, which seems like a lot of trouble to go through for that.

    What was your intent?

    Just as a gentle admonition: A more descriptive title would help...


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Sort files
by japhy (Canon) on Apr 13, 2006 at 14:49 UTC
    No, it shouldn't. Two things pop out. First, you're doing [stat]->[9] again, which as I told you last time is needlessly complex. Use (stat)[9] instead. Second, your sort block refers to $a->[2] and $b->[2] which are undef. They don't exist. Your $a and $b are array refs with only two elements in them ($a->[0] and $a->[1]).

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Sort files
by ahmad (Hermit) on Apr 13, 2006 at 14:47 UTC

    Hello

    why dont you push filenames into array , then sort them whenever you need to deal with them sorted ?

    foreach my $file (sort @files) { ... }

    HTH

      Because it would then look like

      foreach my $file ( map { $_->[0] } sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] } map { [ $_, (stat)[9] ] } @files_Test_flat_file_write_over_name_only ) { ... }

      which is a bit more messy.