sbp has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm a beginner to Perl and programming in general...and need some help.

I have two files, both containing a list of accounts. File 1 has one column, containing the list of accounts and File 2 has 5 columns including filename, accounts, and 3 other nonrelevant columns. I want to retrieve the accounts that're only in File2 (in other words, remove from File2 what is in File1 also)and output the filename that's associated with that account to another file. How can I do this?

Update:

Thank you for your prompt reply. I am also having troubling with the code as it's not compiling.

I have the following errors after I've made the necessary changes to the die statement...

syntax error at test.pl line 11, near "$accounts exists" syntax error at test.pl line 14, near "}"
Also, FILE_2 does not necessarilyy contain account numbers in column2, rather the columns are in the following order:
FILENAME LOC ST CO ACCOUNT
and in this case, the 3 nonrelevant columns are LOC, ST, CO...will the following line work if I make these changes to column order?
my ( $filename, @nonrelevant, $accounts ) = split( /\t/, $line );
I also didn't quite understand what's being done in this statement:
next if ( $accounts exists( @{[<FILE>]} ) );
But I need to output the FILENAME column(for accounts that exist only in FILE_2) into a third file.

Thanks!

#!/usr/bin/perl use strict; use warnings; open FILE_1, 'file1.txt' || die "ERROR:\t$!\n"; open FILE_2, 'file2.txt' || die "ERROR:\t$!\n"; while ( my $line = <FILE_2> ) { my ( $filename, $accounts, @nonrelevant ) = split( /\t/, $line ); next if ( $accounts exists( @{[<FILE>]} ) ); print "$accounts:\t$filename\n"; } close FILE_1; close FILE_2;

20051127 Edit by ysth: prepend original question

Replies are listed 'Best First'.
Re: removing same entries between 2 files
by wfsp (Abbot) on Nov 26, 2005 at 19:08 UTC

    You could load the accounts into a lookup table (a hash), loop over the csv file and test if an account exists. If it doesn't write the data to another file.

    Here is one way you could do that:

    Hope that helps

    update: Tested :-)

Re: removing same entries between 2 files
by wazzuteke (Hermit) on Nov 26, 2005 at 17:37 UTC
    Really, there are a number of different ways to accomplish this task. Perhaps one of the most straight foward methods might be (un-tested):

    #!/usr/bin/perl use strict; use warnings; open FILE_1, 'file.1' || die 'ERROR:\t$!\n"; open FILE_2, 'file.2' || die 'ERROR:\t$!\n"; while ( my $line = <FILE_2> ) { my ( $filename, $accounts, @nonrelevant ) = split( /\t/, $line ); next if ( $accounts exists( @{[<FILE>]} ) ); print "$accounts:\t$filename\n"; } close FILE_1; close FILE_2;
    This is assuming that 'file 2' is tab separated, the layout of the file is as explained, and that I do not know the names or locations of the files.

    Otherwise, this (or something very similar) should search the first file for the account you are working with in the second file, and only print out the account and file name of the accounts that are not within the first file.

    Good Luck!

    ---hA||ta----
    print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );

      This may need some testing!

      The die messages have mismatched quotes.

      You open FILE1 and FILE2 but use FILE.

      And I'm not at all sure what

      next if ( $accounts exists( @{[<FILE>]} ) );
      is all about. It doesn't compile.

      I can see what you're trying to do but it needs a bit more work :-)

        Ouch! Not sure what that was supposed to mean either, I was in a little of a rush and just typed the program into the input form; my apologies. Here is a more 'refined' version of what I meant:

        #!/usr/bin/perl use strict; use warnings; open FILE, 'file.1.dat'; my $hist = {map{ chomp; $_ => 1 } @{[<FILE>]}}; close FILE; open FILE, 'file.2.dat'; while ( my $line = <FILE> ) { chomp( $line ); my ( $file, $account, @other ) = split( /\t/, $line ); next if ( exists( $hist->{$account} ) ); print "$account:\t$file\n"; } close FILE; exit; __END__
        My apologies for the confusion again, and Good Luck!!

        ---hA||ta----
        print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );