I have a tiny script that reads a list of file names on stdin, and outputs the same entries prefixed with modification date, in order of oldest to newest. I have used the script extensively for several years.

Today I noticed that two of the three last lines of output are identical. I wonder if my cygwin setup has been corrupted, or if the latest update has a bug. Can you spot a mistake on my part?

The code creates a hash with file names as keys and timestamps as values. Then it iterates over the keys of the hash, like this:

# read the paths and stat each one while (defined ($_=<>)) { my $f; my @s; ( $f = $_ ) =~ s/\r?\n$//; next unless @s = stat($f); $t{$f}=$s[9]; } # Sort and write to stdout for $_ (sort {$t{$a} <=> $t{$b}} keys %t) { my $f = $_; my $d = POSIX::strftime("%F %T", localtime $t{$_}); if ($dospath) { $f = Filesys::CygwinPaths::win32path($f); } print $d, " ", $f, "\n"; }

(File names are optionally converted to "windows" format "c:\xxx\yyy.zzz" instead of "/cygdrive/c/xxx/yyy.zzz". Timestamps are integers, seconds since end year 1969.)

Even in the case of repeated file names in the input, the output should not have repetitions. (Provided the spelling is the same. I used copy-n-paste from the output into a bash statement [ '' = '' ] && echo equal || echo different in order to make sure I was not overlooking a tiny difference.)

Here are two examples of the output, on the same input data. Notice that the order differs, but there are two files with the exact same Unix timestamp. In every case so far it has been the shorter name that appears repeated. I have not observed any repetitions among files with earlier dates

$ locate zeraia | sort-files-by-date | tail 2017-07-03 14:15:32 C:\Users\Heidi\Dropbox\Teknisk\gpg-db\zeraia-17-07 +-03-14-15.gpg 2017-07-03 16:42:59 C:\Users\Heidi\Dropbox\Teknisk\gpg-db\zeraia-17-07 +-03-16-42.gpg 2017-07-03 16:42:59 C:\cygwin\var\gpg-db\zeraia-17-07-03-16-42.gpg 2017-07-03 21:30:10 C:\Users\Heidi\Dropbox\Teknisk\gpg-db\zeraia-17-07 +-03-21-30.gpg 2017-07-03 21:30:10 C:\cygwin\var\gpg-db\zeraia-17-07-03-21-30.gpg 2017-07-14 18:38:14 C:\cygwin\var\gpg-db\zeraia-17-07-14-18-38.gpg 2017-07-14 18:38:14 C:\Users\Heidi\Dropbox\Teknisk\gpg-db\zeraia-17-07 +-14-18-38.gpg 2017-07-24 14:02:59 C:\cygwin\var\gpg-db\zeraia-17-07-24-14-02.gpg 2017-07-24 14:02:59 C:\cygwin\var\gpg-db\zeraia-17-07-24-14-02.gpg 2017-07-24 14:02:59 C:\Users\Heidi\Dropbox\Teknisk\gpg-db\zeraia-17-07 +-24-14-02.gpg $ locate zeraia | sort-files-by-date | tail 2017-07-03 14:15:32 C:\Users\Heidi\Dropbox\Teknisk\gpg-db\zeraia-17-07 +-03-14-15.gpg 2017-07-03 16:42:59 C:\cygwin\var\gpg-db\zeraia-17-07-03-16-42.gpg 2017-07-03 16:42:59 C:\Users\Heidi\Dropbox\Teknisk\gpg-db\zeraia-17-07 +-03-16-42.gpg 2017-07-03 21:30:10 C:\cygwin\var\gpg-db\zeraia-17-07-03-21-30.gpg 2017-07-03 21:30:10 C:\Users\Heidi\Dropbox\Teknisk\gpg-db\zeraia-17-07 +-03-21-30.gpg 2017-07-14 18:38:14 C:\Users\Heidi\Dropbox\Teknisk\gpg-db\zeraia-17-07 +-14-18-38.gpg 2017-07-14 18:38:14 C:\cygwin\var\gpg-db\zeraia-17-07-14-18-38.gpg 2017-07-24 14:02:59 C:\Users\Heidi\Dropbox\Teknisk\gpg-db\zeraia-17-07 +-24-14-02.gpg 2017-07-24 14:02:59 C:\cygwin\var\gpg-db\zeraia-17-07-24-14-02.gpg 2017-07-24 14:02:59 C:\cygwin\var\gpg-db\zeraia-17-07-24-14-02.gpg

In reply to iterating hash keys sorted by values - last key twice? by Cacadril

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.