First off it look s like you're opening a file then for every line of it opening 
another file.

So if you have two files and file 1 has 100 lines in it you're opening file2 100 times!

That could get messy.

I'd say put file 2 in a hash

Then open file 1 and  do:

if(exists $file1{$f} { ...#
else { .. No #

That would make it so youoonly did two opens ( three if you count writing to the file 
) and
The orig inal prob which was the regex setup and how it saw what it saw.

Doo it that way and I'll bet it goes away.

Also I'd recommend using File::Slurp
So you can make your reads and writes lots easier to manage.

DMuey

> -----Original Message-----
> From: Jay Kidd [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, March 11, 2003 10:40 AM
> To: [EMAIL PROTECTED]
> Subject: Search and Replace
> 
> 
> 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.
> 
> Here's an example:
> 
> File 1:
> dummy.com
> stupid.com
> idiot.com
> newbie.com
> 
> File 2:
> rtfm.com
> readitagain.com
> dummy.com
> stupid.com
> idiot.com
> newbie.com
> 
> New File:
> rtfm.com
> readitagain.com
> #dummy.com
> #stupid.com
> #idiot.com
> #newbie.com
> 
> With the script that I wrote what happens is it only
> comments out the last entry from file 1. so when it
> writes to the new file only (newbie.com) will be
> commented out: 
> 
> New File With My Script:
> rtfm.com
> readitagain.com
> dummy.com
> stupid.com
> idiot.com
> #newbie.com
> 
> Anyone know what I'm doing wrong?
> 
> 
> Here's the script I'm working with:
> 
> #!/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;
> 
> 
> I was able to get all domains commented out by putting
> line in:
> 
> open (NEWDATA, ">>$hostnames.new");
> instead of:
> open (NEWDATA, ">$hostnames.new");
> 
> But it also made duplicate entries in the new text
> file as well. So for every entry that looked like
> this:
> 
> rtfm.com
> readitagain.com
> #dummy.com
> #stupid.com
> #idiot.com
> #newbie.com
> 
> There were 3 more entries that looked like this:
> 
> rtfm.com
> readitagain.com
> dummy.com
> stupid.com
> idiot.com
> newbie.com
> 
> 
> Any guidance on how to fix this problem would be
> greatly appreciated...
> 
> 
> 
> 
> 
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Web Hosting - establish your business online 
http://webhosting.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to