I have tab delim file as follows
#file1
#version1.1
#columns with the information as follows
state1 class1 report_version_1.1 9428 4567 . . cal
+l=1;times=5;toss=head->tail;sno=A1B1;effect=positive
state1 class1 report_version_1.1 3862 4877 . . cal
+l=1;times=5;toss=head->tail;sno=A1B2;effect=negative
state1 class1 report_version_1.1 2376 4567 . . cal
+l=1;times=5;toss=head->tail;sno=;effect=positive
state2 class1 report_version_1.1 4378 2345 . . cal
+l=1;times=5;toss=tail->tail;sno=A1B3;effect=positive,negative, both
state2 class1 report_version_1.1 1289 4835 . . cal
+l=1;times=5;toss=head->tail;sno=;effect=positive
Note: There are no column headers in the file just the three top comments.
I am trying to parse out the 8 column(starting from call=1) which is basically a string separated by semi colons. I need to remove all the entries that have part1: no sno (value/name) (for eg: for 3 row sno=; i.e there is no record) and also part2: those that have same toss results i.e toss=tail->tail is not needed since both are tail.
Since I am learning perl I try and divide such things in parts and hence Here is what I have come up with so far for the part1: sno…
#!usr/bin/perl
use warnings;
#inputfile
my $input_file = "/Users/myfolder/myfile.txt";
die "Cannot open $input_file \n" unless (open(IN, $input_file));
#open file
my @LINES =<IN>;
#output file
#Open output file and write the needed results
die "output1.txt" unless(open( OUT,"> output1.txt"));
my @infos;
while(<IN>){
my @fields = split ';', $_;
my $state = $fields[1];
my $class1 = $fields[2];
my $report_version_1.1 = $fields[3];
my $value1 = $fields[4];
my $value2 = $fields[5];
my $dot1 = $fields[6];
my $dot2 = $fields[7];
my $info = $fields[8];
if ( $fields[8] =~ /^[sno]/ =~ /^[sno]/ ) {
push @infos, $_;
print OUT " $state\ $class\ $report_version_1.1\ $value1\ $value2\ $do
+t1 \ $dot2 \ $info\n";
}
}
exit;
Any other better ways to solve this for both sno and toss parts together?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.