in reply to Re^2: Removing partially duplicated lines from a file
in thread Removing partially duplicated lines from a file
The line of code you asked about basically says if $columns[1] is true (has any value Perl evaluates as true) and contains a string that begins with "HLA-A" then take the following actions. I included the first "does it have a true value" check because I assumed use warnings; would end up complaining for any line that didn't have an element at index 1 in $columns. I didn't actually try it without it, but I just assumed that would happen for at least the all "---" lines.
As for the code changes you requested:
#!/usr/bin/perl use warnings; use strict; open(my $in_fh, '<', 'input.txt') or die $!; my $output; my %seen_lines; while (<$in_fh>) { chomp; my @columns = split; if ($columns[1] and $columns[1] =~ /^HLA-A/) { my $HLA_Peptide = $columns[1] . $columns[2]; $output .= "$_\n" if (!exists $seen_lines{$HLA_Peptide}); $seen_lines{$HLA_Peptide} = 1; } else { $output .= "$_\n"; } } close $in_fh; print $output;
EDIT: I did just try it without that first check and I was correct, it does throw warnings without it. There may be a better way to avoid that warning (it does occur to me that false values like "0" or an empty string would be evaluated as such), but I use this trick a lot in an attempt to appease use warnings; or "-w". I wonder if there is something like exists which I use a lot for hashes only meant for use to check if an array element exists?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Removing partially duplicated lines from a file
by AnomalousMonk (Archbishop) on Jul 27, 2016 at 00:55 UTC | |
by perldigious (Priest) on Jul 27, 2016 at 13:03 UTC | |
by AnomalousMonk (Archbishop) on Jul 27, 2016 at 15:36 UTC | |
by perldigious (Priest) on Jul 27, 2016 at 15:50 UTC | |
|
Re^4: Removing partially duplicated lines from a file
by Sandy_Bio_Perl (Beadle) on Jul 26, 2016 at 21:35 UTC |