Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 22nd, 2001, 12:40 AM
SeltaR SeltaR is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 0 SeltaR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old June 22nd, 2001, 02:59 PM
mullaney mullaney is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 0 mullaney User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > sorting files according to contents


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway