I suspect you are new to Perl, the comments in the code below are intended to be educational. I have used a couple of things you may not have seen before,
@array[2..$#array] is a slice of an array, it can be thought of as a sub array starting at index 2 and going to the last element of the array
$#array is the last index of @array
Ok, so you have the individual records in the @data array.
You now need to split the records and check that any of the values in the resultant array with an index > 2 are not equal to zero. If there are print the line.
something like the following is what you are looking for.
use strict; # These two lines
use warnings;# will save you grief
open (my $data_file, "<", 'test.tsv'); # better to use 3 argument ver
+sion of open and to have a lexical filehandle
my @data = <$data_file>; # scope your variables
close( $data_file); # It's only polite to close up after yourself
print $data[0]; # headers
for my $line (@data[1..$#data]){ # More perlish not to use indices dir
+ectly
my @line = split(/\t/, $line); # split the values into an array
print $line if (check_for_zero(@line)); # Use a routine to evaluate
+ the elements
}
sub check_for_zero{ # declare routine
my @line = @_; # get the array we're called with
for my $field (@line[2..$#line]){ # again more perlish
return 1 if $field != 0; # We have a non-zero value
}
return 0; #if we got here we never met a non-zero value
}
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
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.