carrerag has asked for the wisdom of the Perl Monks concerning the following question:
And here is the code:lat="37.4192" lng="-122.0574" United States ID No: 1123631397
After the first run, the data in data.txt appears to have the correct result. However, I was expecting that purge.txt would contain all lines that were removed as duplicates but, it only contains one line. Subsequent runs _always_ remove the first line of data from input.txt and places it in purge.txt. Could someone please point out my error? I'm trying to teach myself Perl but for some reason I can't get a grasp on what's going wrong here.open (FILE, "<:utf8", "input.txt"); my @lines = <FILE>; my @uniq = (); my @purge = (); my %seen = (); foreach $line (@lines) { my $id = $line =~ m/ID No: (\d+)/; if ($seen{$id}++){ push (@uniq, $line); $new_uniq++; } else { push (@purge, $line); } open (MYFILE, ">:utf8", "data.txt"); print MYFILE @uniq; open (PURGE, ">>:utf8", "purge.txt"); print PURGE @purge;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Removing duplicate lines based on a match
by FunkyMonk (Bishop) on Jun 19, 2008 at 16:54 UTC | |
|
Re: Removing duplicate lines based on a match
by kyle (Abbot) on Jun 19, 2008 at 17:16 UTC | |
|
Re: Removing duplicate lines based on a match
by Fletch (Bishop) on Jun 19, 2008 at 17:18 UTC | |
|
Re: Removing duplicate lines based on a match
by wfsp (Abbot) on Jun 19, 2008 at 17:09 UTC | |
|
Re: Removing duplicate lines based on a match
by carrerag (Initiate) on Jun 20, 2008 at 20:05 UTC |