Hello monks!
I have two files, in the following formats:
FILE1
peter 1234
nick 1111
john 4567
mike 3333
george 2222
antony 5632
migel 1209
FILE2
john 7559
mike 3333
george 2222
peter 5643
nick 1111
julia 3456
What I want to do is:
Read through FILE1 and store name and number in a hash, call it %hash1
[I have done it]
Foreach $key of %hash1, I must check if it exists in FILE2. If it exists, I must then check if the number in FILE1
[$hash1{$key}] is the same with the number in FILE2 {$hash2{$key}}. If it is the same, I print $key."\t".'OK'."$hash1{$key}";
and if the numbers don't match, I will print $key."\tWRONG".$hash1{$key}.
Also, if a $key of $hash1 does not exist in FILE2, I must print it.
To be more clear, I must have:
peter WRONG 1234
nick OK 1111
john WRONG 4567
mike OK 3333
george OK 2222
antony WRONG 5632
migel WRONG 1209
I have written:
open ONE, $FILE1;
while (<ONE>)
{
chomp;
if ($_=~/^(.*)\t(.*)/)
{
$hash1{$1} =$2;
}
}
foreach $key(keys %hash1)
{
open TWO, $FILE2;
while (<TWO>)
{
chomp;
if ($_=~/^$key\t(.*)/)
{
if ($1 eq $hash1{$key})
{
print $key."\t".'OK'."$hash1{$key}"."\n";
}
else
{
print $key."\t".'WRONG'."\t".$hash1{$key}."\n";
}
}
}
}
What I cannot do is print all names of FILE1 that ARE NOT in FILE2.
Any hints?
Code tags added by GrandFather
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.