Okay, you have combined two separate methods of reading the lines in the file. Pick one. They are functionally identical, but I recommend the latter because it doesn't require the WHOLE file to be in memory at any given time.
...
$db = "textfile.txt";
open(DATA, $db) or die "cant open: $!\n";
@dat = <DATA>;
close(DATA);
foreach $line (@dat)
{
...
}
...
$db = "textfile.txt";
open(DATA, $db) || die "cant open: $!\n";
foreach $line (<DATA>)
{
...
}
close(DATA);
The instances of
... mark the areas where you're hoping for some help. You only care about fields 2 and 5 of each line. You either want to print any line that has already been seen, or you want to print any line that has
not already been seen.
Break down the problem further.
- You need to keep track of what's been seen in some kind of data structure. (I hinted a hash.)
- You need to test each line in the file against the data structure to see if it's been seen before, or not.
- You need to decide whether to print the line or not.
- You need to add the crucial fields to the data structure so your future iterations have something to check.
Again, I'm treating this like it's homework, and drawing you through the thinking process, rather than just handing you a solution. If you just want to be given code, I'm sure some other folks are happy to grant your wish.
--
[ e d @ h a l l e y . c c ]
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.