in reply to Re: Removing duplicates
in thread Removing duplicates

I have a file "file1" that contains: dave bob rich jim I have a second file "file2" that contains: dave rich I need "file2" listing to remove lines from "file1" to create a "file3" that contains only: bob jim I tried this code: #!/usr/bin/perl use strict; # use warnings; open(MYOUTFILE, ">file3"); open(MYOUTFILE, ">>file3"); my $file = shift or die "Usage: $0 file1 < file2"; my $file_contents = do { local (@ARGV, $/) = $file; <> }; my $nets = join "|", map {chomp;$_} $file_contents; /$nets/ or print MYOUTFILE while <STDIN>; close(MYOUTFILE); But it does not get me my example of file3 should look like. Help! Thanks.. RCP

Replies are listed 'Best First'.
Re: Re: Re: Removing duplicates
by talexb (Chancellor) on Mar 05, 2004 at 18:36 UTC

    From the Perl debugger session I just ran:

    [alex@rand alex]$ perl -de 1 Loading DB routines from perl5db.pl version 1.19 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 1 DB<1> @file1=qw/dave bob rich jim/; DB<2> @file2=qw/dave rich/; DB<3> foreach(@file1){$list{$_}=1;} DB<4> foreach(@file2){delete $list{$_};} DB<5> print "Remaining elements " . join("; ",keys %list) . "\n"; Remaining elements jim; bob DB<6>
    You can fiddle with the 'list' hash as you read the file, so there's no need to use an array as the middle man.

    Alex / talexb / Toronto

    Life is short: get busy!

    PS: When posting code, put it in between 'code' tags. That way it doesn't wrap like normal text.

      Thanks for yout inputs. The below code is how I got the output I wanted.
      #!perl open FILE1, "<file1"; @linesFile1 = <FILE1>; close FILE1; open FILE2, "<file2"; @linesFile2 = <FILE2>; close FILE2; open(MYOUTFILE, ">file3"); open(MYOUTFILE, ">>file3"); foreach $line(@linesFile1) { if(! grep{$_ eq $line} @linesFile2 ){ print MYOUTFILE "$line"; } } print "\n"; close(MYOUTFILE);
      Thanks again.. RCP