Hi,
I'm having trouble with the following. I'm relatively new to Perl so any help would be appreciated
I have two files e.g.
file 1
------
a 0
c 1
d 1
e 3
f 0
file 2
------
a 0
b 0
c 0
d 0
e 0
f 0
I need to compare column 1 of file2 with column1 of file1
If file2 has an extra record that isn't in file1 then a new file needs to be created which is a copy of file1 but has this new record in (in sorted order)
e.g.
file3
-----
a 0
b 0
c 1
d 1
e 3
f 0
The code I have for this so far (which doesn't work) is
open FIRST,"file1" or die "Can't open file1: $!\n";
open LAST,"file2" or die "Can't open file2: $!\n";
open NEW,">file3" or die "Can't open file3: $!\n";
chomp (my @last=<LAST>);
my %names;
@names {<FIRST>} = ();
foreach my $name (@last) {
next if exists $names { $name }or
print NEW "$name\n";
}
close FIRST;
close LAST;
close NEW;
This is just creating file3 as a complete copy of file2.
How do I get it to just compare the first element of each record ?
How do I get the new record inserted in the correct order in the new file ?
Any help appreciated, I'm really struggling with the logic on this one
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.