in reply to delete redundant data
Whenever you want to detect duplicates and/or unique values, one thing that should come to mind is the hash. Since it maps a key to a unique value. So in your case, you can just build a key for each incoming record. If the key does not exist in the hash, you process the record, ignoring it otherwise. Then be sure to enter the key into the hash.
Here's a quick[1] modification to your program to use a hash to detect and eliminate duplicate values:
#!/usr/bin/perl use strict; use warnings; # Records separated by blank line $/ = "\r\n\r\n"; # Records we've seen before my %records_seen; while (my $line = <DATA>) { # Get list of key fields for record my @key_fields = (split /\s+/, $line)[ -2, -8, -14, -5, -11, -17 ]; # Create composite key for record my $key = join("|",@key_fields); # Process the record if we haven't seen it before if (! exists $records_seen{$key}) { print $line; } # Remember that we've processed the record $records_seen{$key} = $line; } __DATA__ A 83 GLU A 90 GLU^? A 163 ARG A 83 ARG^? A 222 ARG A 5 ARG^? A 229 ALA A 115 ALA~? A 257 ALA A 118 ALA~? A 328 ASP A 95 ASP~? A 83 GLU A 90 GLU^? A 163 ARG A 83 ARG^? A 222 ARG A 5 ARG^? A 83 GLU B 90 GLU^? A 163 ARG B 83 ARG^? A 222 ARG B 5 ARG^?
Running this gives us:
Roboticus@Roboticus-PC /robo/Desktop $ perl 856427.pl A 83 GLU A 90 GLU^? A 163 ARG A 83 ARG^? A 222 ARG A 5 ARG^? A 229 ALA A 115 ALA~? A 257 ALA A 118 ALA~? A 328 ASP A 95 ASP~? Roboticus@Roboticus-PC /robo/Desktop $
Here are some assorted notes on your code, to explain why there are so many differences between your code and mine:
This would still take two lines, is no harder to understand, and splits the line a single time.my ($i_new, $j_new, $k_new, $l_new, $m_new, $n_new) = (split /\s+/,$line)[ -2, -8, -14, -5, -11, -17 ];
...roboticus
Update: Added this footnote:
[1] Apparently not *very* quick, as baxy77bax posted a similar reply some 30 minutes earlier as I was composing this node...
Update: Corrected "If the key exists in the hash" to "If the key does not exist in the hash" in the first paragraph.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: delete redundant data
by nurulnad (Acolyte) on Aug 21, 2010 at 17:01 UTC | |
|
Re^2: delete redundant data
by nurulnad (Acolyte) on Aug 22, 2010 at 00:20 UTC | |
by roboticus (Chancellor) on Aug 22, 2010 at 14:05 UTC |