Perl

Moderators: Jonathan
Number of threads: 1257
Number of posts: 3636

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Duplications remover. Posted by Hav0c #1 on 16 May 2010 at 10:40 AM
Hey all.
Okay I am kind of new to PERL.

I have 2 files A.txt and B.txt now I compare A.txt to B.txt and if file B.txt doesn’t have the word in it that file A.txt has, file B.txt gets updated with the new word from file A.txt

This is the problem that I have, file A.txt has the word AAAS duplicated a number of times and file B.txt does not have the word in it.
The first time around B.txt gets updated with the new word AAAS and then when A.txt compares AAAS in B.txt, B.txt gets updated again with the word AAAS.
Duplicating the word AAAS in file B.txt as well.

How do I stop this from happening??

Following is the code that i am using. . .

my($comepare_word);
my($compare_dic_word);
my($flag) = "false";

sub trim($)
{
	my $string = $_;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}

open compareWord, "<A.txt" or die "Main file cannot be opend $!";

	while(<compareWord>) {
 		chomp;
 		$comepare_word = trim($_);

		$first_char = substr($comepare_word, 0, 1);

 		if ($first_char eq "a" || $first_char eq "A" ) {
			open compareDic, "<B.txt" or die "B.txt cannot be opend $!";
			while(<compareDic>) {
					chomp;
  					$compare_dic_word = trim($_);
					if($comepare_word eq $compare_dic_word) {
						$flag = "true";						
						last;
					} else {
						$flag = "false";
					}		
		}

		if($flag eq "false"){
			open outFile, ">>B.txt" or die "OUT cannot be opend $!";			
			print outFile "$comepare_word\n";
			$flag = "false"; 			
 	 	}
	}
}
Report
This post has been deleted. Posted by acetechcn on 31 May 2010 at 11:46 PM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.