Ok, based on what I've read here, the first thing that comes to mind to handle the date is to parse out the 3 fields that comprise the date. One factor that you may be missing is that in general, files older than a year don't show the time, instead they show like this: (from my Solaris workstation):
drwxr-x--- 4 root root 6 Jan 29 11:03 data
drwxr-xr-x 2 root root 4 Nov 6 2008 Desktop
One option, at least on Solaris, (I don't know if all flavors of unix share this parameter) you can use -e to list the time in a standard format regardless of how old the file is:
drwxr-x--- 4 root root 6 Jan 29 11:03:30 2010 data
drwxr-xr-x 2 root root 4 Nov 6 11:54:50 2008 Desktop
Now, that being said, you know what columns comprise the date, you can capture those (you can use split to separate by whitespace, then use columns 6, 7, 8, 9 for the date in this case) and build the date yourself into a format that Date::Calc (or whatever date function you want) can read.
Once you get to that point, you should have all of the information you need to perform the sort functions that you like. I've never used Sort::Fields, but if its an easy framework to use, I'm sure it shouldn't be too difficult to set up to sort on whatever column you click on.
For the data structure, again the first thing that comes to mind is an array of hashes. Each hash representing a line in the listing (or it could be an array of arrays, but I like having non-numeric keys) and the array would represent that directory's contents.
I can't imagine a good reason for sorting on the permissions, but I suppose it could be done. They are alphanumeric characters, and are in order (r < w < x) to match the sequence shown. Doing queries to see if a file is executable,writable,etc could easily be done with a regex on field 2 (note in my listing on Solaris 10, the first field is the permissions, and second field are the links -- not sure why yours are different, haven't looked into it) but whether or not you want to do that is entirely up to you of course.
Hope that helps! | [reply] [d/l] [select] |
Hi Craig,
You need to use different sorts based on whether this is numeric , alphanumeric or timestamp data.
I would suggest you use a dispatch table as your sort block, ie:
sub sortFiles{
my ($field, @data)=@_;
my $sortFunction={
timestamp => \&dateSort,
filename => \&alphasort,
group => \&alphasort,
owner => \&alphasort,
permissions => \&permSort,
}
return sort $sortFunction{$field} @data;
}
The permissions I'd sort them by their numeric equivalent.
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
| [reply] [d/l] [select] |
While you are constrained as to the kind of input you get, maybe you don't need to stick rigorously to that constraint when presenting the info to the user. I would seriously consider editing the input list to normalize the date field to a consistent form, so that sorting on the date field will be trivial (as opposed to being complicated).
As suggested in an earlier reply, you should (if at all possible) use the SunOS 'ls' option that yields a consistent date format in its output, then use one of the many Date::whatever modules (or a regex) to convert that to YYYY-MM-DD HR:MI:SC, which doesn't need any special treatment when sorting (ascii-betic sort == chronological sort). People ought to find this display format easy enough to read, and you can use that edited form of the data for both sorting and display.
Don't hesitate to allow sorting on the permission field -- an (ascending or descending) ascii-betic sort on that can be surprisingly handy in some situations (e.g. to understand why some people aren't able to see the contents of some files...)
Meanwhile, the link-count and block-count fields are relatively worthless -- if you're going to be editing the 'ls' output anyway (to make the date field manageable), I would take those two out completely; don't even show them to a GUI user. | [reply] |