<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2657.73">
<TITLE>RE: [Kc] sort by mod date [x-adr][x-sls]</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>jyoung79 at kc dot rr dot com wrote:</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> I'm new to Perl and I was just curious if anyone here had</FONT>
<BR><FONT SIZE=2>> any thoughts about the code below. I'm running this on OS</FONT>
<BR><FONT SIZE=2>> X (currently using an app called 'Affrus') and it sorts</FONT>
<BR><FONT SIZE=2>> the files by modification date. Here's my original code:</FONT>
<BR><FONT SIZE=2>...</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> I asked this question also on the OS X Perl List and Jarkko</FONT>
<BR><FONT SIZE=2>> had sent this:</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> -------</FONT>
<BR><FONT SIZE=2>> opendir(DIR, shift || ".") && print map { "$_->[1]\n" }</FONT>
<BR><FONT SIZE=2>> sort { $a->[0] <=> $b->[0] || $a->[1] cmp $b->[1] }</FONT>
<BR><FONT SIZE=2>> map { [ (stat($_))[9], $_ ] } grep { !/^\.{1,2}$/ }</FONT>
<BR><FONT SIZE=2>> readdir(DIR)</FONT>
<BR><FONT SIZE=2>> -------</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> which I thought looked really cool, so I changed it a bit to:</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> -------</FONT>
<BR><FONT SIZE=2>> opendir(DIR, shift || "/Users/jay/Desktop/Other Stuff/old stuff 4")</FONT>
<BR><FONT SIZE=2>> && print map { "$_->[1]\n" } sort { $a->[0] <=> $b->[0] </FONT>
<BR><FONT SIZE=2>> || $a->[1] cmp $b->[1] } map { [ (stat($_))[9], $_ ] }</FONT>
<BR><FONT SIZE=2>> grep { !/^\.{1,2}$/ } readdir(DIR)</FONT>
<BR><FONT SIZE=2>> -------</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> but I never could make it work. It kept giving me these errors:</FONT>
<BR><FONT SIZE=2>> </FONT>
<BR><FONT SIZE=2>> * Argument (file name) isn't numeric in numeric comparison (<=>)</FONT>
<BR><FONT SIZE=2>> * (62) Use of uninitialized value in string comparison (cmp)</FONT>
</P>
<BR>
<P><FONT SIZE=2>The script works for me on a Win32 system. Did you cut and past from your script or re-type it? </FONT>
</P>
<BR>
<P><FONT SIZE=2>The part which looked odd to me on my initial reading was:</FONT>
</P>
<P><FONT SIZE=2>> sort { $a->[0] <=> $b->[0] || $a->[1] cmp $b->[1] }</FONT>
</P>
<P><FONT SIZE=2>I haven't seen that before. But I guess I can see how it's useful in this case. It basically means sort numerically by file modification time, sorting further by filename where the modification times are equal.</FONT></P>
<BR>
<P><FONT SIZE=2>You might see what the following produces:</FONT>
</P>
<P><FONT SIZE=2>opendir(DIR, shift || "/Users/jay/Desktop/Other Stuff/old stuff 4");</FONT>
<BR><FONT SIZE=2>for my $i (grep { !/^\.{1,2}$/ } readdir(DIR)) {</FONT>
<BR><FONT SIZE=2> print '['. (stat($i))[9] . ", $i]\n"</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>
<P><FONT SIZE=2>That'll print out the contents of the list of anonymous array references which are being sorted. For me it looked something like:</FONT></P>
<P><FONT SIZE=2>[1087217796, 1]</FONT>
<BR><FONT SIZE=2>[1087217802, 2]</FONT>
<BR><FONT SIZE=2>[1087217798, 3]</FONT>
<BR><FONT SIZE=2>[1087217794, a]</FONT>
<BR><FONT SIZE=2>[1087217800, b]</FONT>
<BR><FONT SIZE=2>[1087217790, c]</FONT>
<BR><FONT SIZE=2>[1087219921, kcpm.pl]</FONT>
</P>
<P><FONT SIZE=2>I don't imagine it'll look the same for you... or else you wouldn't be getting that "isn't numeric" error when making a comparison like {$a->[0] <=> $b->[0]} where for the first two elements in my example, the block should reduce to {1087217796 <=> 1087217802}.</FONT></P>
<P><FONT SIZE=2>--</FONT>
<BR><FONT SIZE=2>Garrett Goebel</FONT>
<BR><FONT SIZE=2>IS Development Specialist</FONT>
</P>
<P><FONT SIZE=2>ScriptPro Direct: 913.403.5261</FONT>
<BR><FONT SIZE=2>5828 Reeds Road Main: 913.384.1008</FONT>
<BR><FONT SIZE=2>Mission, KS 66202 Fax: 913.384.2180</FONT>
<BR><FONT SIZE=2>www.scriptpro.com garrett@scriptpro.com</FONT>
</P>
</BODY>
</HTML>