I want to count the number of the lines that contains keyword "energy" and the number of the lines that occur between the keyword "energy". The input file is as follows
536 ENERGY = -176.2 gag
1 G 0 2 0 1
2 G 1 3 533 2
3 G 2 4 532 3
536 ENERGY = -175.9 gag
1 G 0 2 0 1
2 G 1 3 533 2
3 G 2 4 532 3
As in this case the code should print 2 and 3 respectively.
My code is just printing the number of lines containing "energy" which is 2 in this case. but not printing the number of lines within "energy" which is 3. My code is as follows:
open(MYFILE, $data_file) || die("Could not open file!");
@raw_data=<MYFILE>;
foreach $c (@raw_data)
{
@c = $c;
(@c[0],@c[1],@c[2], @c[3], @c[4], @c[5], @c[6])=split(/\s+/,$c);
if(@c[2] eq ENERGY)
{
#print "energy found. reading next line\n";
while(@c[2] ne ENERGY)
{
$_=<MYFILE>;
print $_;
$count = $count + 1;
}
}
}
print "$count\n";
close(MYFILE);
Can anyone please help me?I guess there is something wrong with while loop. Your help will really be appreciated.
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.