|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
sorting files according to contents
I am trying to make a bulletin board system.
I need to order the posts by the times by which they were posted. So lets say I have posts: 1.txt --> time: 6/15/2001 2.txt --> time: 6/9/2001 3.txt --> time: 6/19/2001 Sorting them greatest to least I get 3, 2, 1. Sorting them by time (inside the file) I need to get 3, 1, 2. Time is inside the file like this field~field~field~field~field~TIME HERE So what I need to do is open each file and retrieve time from each of them. Then sort the times greatest to least, then "revert" back so that I am sorting the post numbers. I have an idea on how to do it, but I have no idea how to code it. Any help is appreciated. |
|
#2
|
|||
|
|||
|
Here is a small script I created on Linux. It can use some cleaning up, but it works. It assumes...
Filename: filename.txt Contents: field~field~field~field~field~yyyymmdd I would recommend reformatting your date so that it follows the form yyyymmdd. That would be handier to sort. Otherwise, you could do some substituting to get it in a form suitable for sorting. Hope this helps, Bob #!/usr/bin/perl -w $dir = "/dir"; @files = glob ("$dir/*.txt"); foreach $file (@files) { open (FH, "$file"); my $line = <FH>; chomp $line; my @parts = split (/~/,$line); $time{$parts[$#parts]} = $file; # Build %time hash. close (FH); } foreach $key (sort (keys %time)) { print "$time{$key} = $key\n"; } exit; |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > sorting files according to contents |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|