As usual, there are several ways to do it. In the sample code I've taken care of it by tagging the time value onto the end of the date string, aligning it such that the whole string can simply be sorted asciibetically to yield the proper result. Note that space (ASCII 32) orders before digits (ASCII 48..57).  This is done with the sprintf("%s %6d",$date,$time).

With the following sample input

__DATA__ 0.ids.xml 500 2004-10-01 1.ids.xml 2 2004-10-01 2.ids.xml 30 2004-10-01 3.ids.xml 600 2004-10-01 4.ids.xml 40 2004-10-01 5.ids.xml 7000 2004-10-01 6.ids.xml 8000 2004-10-01 7.ids.xml 1 2004-10-01 8.ids.xml 100000 2004-10-01 9.ids.xml 90000 2004-10-01

and the comparison operation as shown — $b->[1] cmp $a->[1] (string sort, reversed) — this would order as

2004-10-01 100000 2004-10-01 90000 2004-10-01 8000 2004-10-01 7000 2004-10-01 600 2004-10-01 500 2004-10-01 40 2004-10-01 30 2004-10-01 2 2004-10-01 1

i.e. you get the entry with the highest time value as the first element.

Another way would be to store the date and time values separately

push @{$hash{$common}}, [ $input_file, $date, $time ];

and then use a generic chained sort operation

@files = sort {$b->[1] cmp $a->[1] || $b->[2] <=> $a->[2]} @{$hash +{$k}};

This works because if the date value is equal, the first comparison ($b->[1] cmp $a->[1]) evaluates to zero, so the next comparison ($b->[2] <=> $a->[2]) after the logical or "||" is tested to determine if the time differs (it kind of "falls through"). Note that in this case the time value must be compared numerically, i.e. with <=>, or else (with string comparison cmp) the 100000 would be ordered in between 1 and 2.   See sort.


In reply to Re^3: Delete the file with checking the value by almut
in thread Delete the file with checking the value by Anonymous Monk

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.