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

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

Replies are listed 'Best First'.
Re: iterating hash keys sorted by values - last key twice?
by huck (Prior) on Aug 07, 2017 at 01:41 UTC

    I would love to see the output when the end of the first loop is changed to

    $t{$f}=$s[9]; print $f."\n"; }
    Im betting you will see two filenames, one with a forward slash where the other has a backwards slash

    with different slashes, the keys would be different, but after the dos conversion they would look the same

Re: iterating hash keys sorted by values - last key twice?
by ikegami (Patriarch) on Aug 07, 2017 at 01:30 UTC

    What exactly is the problem? What's the input giving this problem?

    Duplicate names in the output are possible if you two names resolve to the same Windows path.