Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

sort file list

by davestiff (Initiate)
on Apr 27, 2004 at 19:47 UTC ( #348622=perlquestion: print w/ replies, xml ) Need Help??
davestiff has asked for the wisdom of the Perl Monks concerning the following question:

I have a list of files that look like:
1999-12-07 12:00p 3,856 abc.dll 2002-02-24 09:30p 260,096 xyz.dll 2004-03-19 01:29p 992,907 lmo.dll 2004-04-08 03:23p 24,576 efg.dll 2004-03-11 08:04a 1,036,288 stu.dll
(DOS output) I would like to alpha sort them based on file name. What would be the best way to do that? (Go easy on me. I learned Perl from a 21 days book.) Thanks, Dave

Comment on sort file list
Download Code
Re: sort file list
by davido (Archbishop) on Apr 27, 2004 at 19:54 UTC
    This particular method is tried and proven, and should get you going in the right direction.

    my @files = <DATA>; chomp @files; my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_ , ( split /\s+/, $_ )[-1] ] } @files; print "$_\n" for @sorted; __DATA__ 1999-12-07 12:00p 3,856 abc.dll 2002-02-24 09:30p 260,096 xyz.dll 2004-03-19 01:29p 992,907 lmo.dll 2004-04-08 03:23p 24,576 efg.dll 2004-03-11 0804a 1,036,288 stu.dll

    For an explanation, look up Schwartzian Transform.

    This is, of course, just One Way To Do It. Here come the rest..........


    Dave

Re: sort file list
by matija (Priest) on Apr 27, 2004 at 19:54 UTC
    First you have to decide how you'll get the name of the file from that line. Since it's the last entry on the line, you could use split:
    @arr=sort {split(/\s+/,$a)[-1] cmp split(/\s+/,$b)} @arr;
    Note that this code is very inefficient: it splits each string each time it has to make a comparison during sorting. But you should be alright as long as you only have hundreds of files in a listing. It also fails if the filenames have blanks in them.

    For a more serious routine, you could pick the line apart with a regular expression, change the array into a list of lists that contains the filename as one element and the original line as the other, sort based on the filename element, and in the final step throw the filename away, leaving the sorted list of lines. This is known as Schwartzian transform.

Re: sort file list
by Limbic~Region (Chancellor) on Apr 27, 2004 at 19:55 UTC
    davestiff,
    I am not sure I understand. Do you have a file that contains lines that look like you have shown and want to sort them alphabetically by file name?
    #!/usr/bin/perl use strict; use warnings; my %file; while ( <DATA> ) { chomp; my @info = split " "; $file{ $info[3] } = $_; } print "$file{$_}\n" for sort keys %file; __DATA__ 1999-12-07 12:00p 3,856 abc.dll 2002-02-24 09:30p 260,096 xyz.dll 2004-03-19 01:29p 992,907 lmo.dll 2004-04-08 03:23p 24,576 efg.dll 2004-03-11 08:04a 1,036,288 stu.dll
    Or do you have a list of files that you want to get additional information for in alphabetical order:
    #!/usr/bin/perl use strict; use warnings; for ( sort glob "*.dll" ) { print "$_ : ", -s, "\n"; }
    See perldoc -f stat for more information if this is what you were after.

    Cheers - L~R

Re: sort file list
by Anonymous Monk on Apr 27, 2004 at 21:33 UTC
    Just in case you used the MS-DOS dir command to get this list: Have a look at dir /? and try dir [...] /on which sorts the files in alphabetical order.
Re: sort file list
by cyocum (Curate) on Apr 27, 2004 at 22:48 UTC

    Do not forget that you can use the glob function to get all the file names that end in .dll then use the file test operators to get that information then do the alpha sort.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://348622]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2014-09-01 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (13 votes), past polls