sbp has asked for the wisdom of the Perl Monks concerning the following question:
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...
Also, FILE_2 does not necessarilyy contain account numbers in column2, rather the columns are in the following order:syntax error at test.pl line 11, near "$accounts exists" syntax error at test.pl line 14, near "}"
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?FILENAME LOC ST CO ACCOUNT
I also didn't quite understand what's being done in this statement:my ( $filename, @nonrelevant, $accounts ) = split( /\t/, $line );
But I need to output the FILENAME column(for accounts that exist only in FILE_2) into a third file.next if ( $accounts exists( @{[<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 | |
|
Re: removing same entries between 2 files
by wazzuteke (Hermit) on Nov 26, 2005 at 17:37 UTC | |
by wfsp (Abbot) on Nov 26, 2005 at 19:15 UTC | |
by wazzuteke (Hermit) on Nov 28, 2005 at 02:55 UTC | |
by sbp (Initiate) on Nov 30, 2005 at 03:12 UTC | |
by BrowserUk (Patriarch) on Nov 30, 2005 at 03:45 UTC |