CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Beginner - Sort

 



adp
newbie

Feb 21, 2001, 7:24 AM

Post #1 of 3 (632 views)
Beginner - Sort Can't Post

Hi!

I'm have a simple simple sort question-
How can I sort a simple text file, (deleting duplicates) and print the sorted file?



Jean
User


Feb 21, 2001, 7:48 AM

Post #2 of 3 (629 views)
Re: Beginner - Sort [In reply to] Can't Post

It depends on how large your file is. In case it's a small file, you can simply read all its lines into an array, sort it and then dump it back to the file.

Regarding removing duplicates, I'm attaching two sample scripts that do the duplicate elimination.
The simpler one removes only sequential duplicates, i.e. can be used after sorting only and the other one removes all duplicates, though with larger files it is slower.


Code
# REMOVES SEQUENTIAL DUPLICATES ######## 
my $SourceFile = $ARGV[0] || "Server.log";
my $TargetFile = $SourceFile.".clean";
my $lastline;

# Open the files
open (SOURCEFILE, "<$SourceFile") or die "Can't open $SourceFile";
open (TARGETFILE, ">>$TargetFile") or die "Can't create $TargetFile";

while (<SOURCEFILE>)
{
if ( $_ ne $lastline )
{
print TARGETFILE $_;
}
$lastline = $_;
}

# Close the files.
close (SOURCEFILE);
close (TARGETFILE);

# REMOVES ALL DUPLICATES ########
my $SourceFile = $ARGV[0] || "Server.log";
my $TargetFile = $SourceFile.".NoDups";
my $i;

# Open the files
open (SOURCEFILE, "<$SourceFile") or die "Can't open $SourceFile";
open (TARGETFILE, ">>$TargetFile") or die "Can't create $TargetFile";

while (<SOURCEFILE>)
{
chomp;
if ( !exists $lines{$_} )
{
$i++;
$lines{$_} = "1";
print TARGETFILE "$_\n";
}
}

# Close the files.
close (SOURCEFILE);
close (TARGETFILE);

Note from japhy -- use the <pre> and </pre> tags around your code block (except use [ and ] instead of < and >)

Jean
QA Engineer @ http://www.extent.com
mage@lycosmail.com


(This post was edited by japhy on Feb 21, 2001, 8:17 AM)


adp
newbie

Feb 22, 2001, 6:52 AM

Post #3 of 3 (614 views)
Re: Beginner - Sort [In reply to] Can't Post

Thanks much!
:)


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives