Jay Kidd wrote: > Hello, > > I need help trying to figure out what I'm doing wrong. > I'm working with 2 seperate text files both of which > contain domain names. What I'm attempting to do is > read the first file of domains and run a search based > on the contents of that file against the second file > of domains. Then open a completely new file writing > the contents of the second file with the exception of > the domains from the first file will be commented out.
Hi Jay, First things first. In order to get even a clue as to what your code was about, I reformatted the whole thing. Your indentation was totally random as posted. Please use a code editor that indents with spaces, and adopt a consistent indentation pattern. I'm going to post your code in full, then look at particular sections: Full code: #!/usr/bin/perl -w use strict; my $domainlist = shift || die("You must provide a domain list file."); my $hostnames = "local-host-names"; open(DAT, "<$domainlist") || die("Unable to open'$domainlist'."); while (<DAT>) { chomp; print $_,"\n"; my $cur1 = $_; open (DATA, "<$hostnames"); open (NEWDATA, ">$hostnames.new"); while (<DATA>) { chomp; my $cur2 = $_; my ($data) = $cur2 =~ /($cur1)/is; my $newdata; if ($data) { $newdata = "#" . $cur2; } else { $newdata = $cur2; } print NEWDATA "$newdata\n"; } close DATA; close NEWDATA; } close DAT; ************************* Now let's look at some of what goes on here: open(DAT, "<$domainlist") || die("Unable to open'$domainlist'."); How about: open(BANNED, "<$domainlist") || die("Unable to open'$domainlist'."); open (DATA, "<$hostnames"); How about: open (HOSTNAME, "<$hostnames"); Substituting these identifiers in so that you can see what is happening in the code, it should quickly become clear what is going on. You are moving through the banned domains file. For each domain in the banned domains file, you are examining the entire original hosts file, and writing an entire new file, which comments out the host entry for that banned domain. overwriting the previous versions of the new file, which had had the previous banned domain commented out. You logic is inside out. You must restructure. You will use the same information from the banned domains file each time you examine a line from the hosts file. There fore it makes sense to get these files into the program and keep them there: my @banned; open (BANNED, "$domainlist") or die("Unable to open $domainlist $!"); while (<BANNED>) {push @banned, $_;} close BANNED; That will allow you to eliminate the outer loop entirely. To test each line of the hostnames file, you can then iterate through the array. If a line in the banned array matches, you would set a isBanned flag to true, then last out of the loop to print the line without comment. If the hostname makes it through the banned array without matching, the isBanned never gets set, and the line is printed to the new file as is. Try to work with this. Do a little logical restructuring, then indent to reflect the logical flow of your code. Let us know what you come up with. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]