I want to add the occurrence of the "energy" at the end of each of the printed line. My input file is as follows

536 ENERGY = -176.2 gag
1 0 2 0
2 1 3 533
3 2 4 532
536 ENERGY = -175.9 gag
1 0 2 0
2 1 3 533
3 2 4 532
My code is as follows
open(MYFILE, $data_file) || die("Could not open file!"); $b=$between=$energy=0; while (<MYFILE>) { foreach $line (<MYFILE>) { if (/\bENERGY\b/) { ++$energy; #print "$energy\n"; @line = $line; (@line[0],@line[1],@line[2], @line[3], @line[4], @line[5], @line[6])=s +plit(/\s+/,$line); if(@line[2] ne ENERGY) { if (@line[3] != 0) { print "@line[1] @line[3] \t $energy\n"; } if (@line[4] != 0) { print "@line[1] @line[4] \t $energy\n"; } if(@line[5] != 0) { print "@line[1] @line[5] \t $energy\n"; } } } } } close(MYFILE);

I have taken array for splitting the columns so that first integer in every row makes a pair with 2nd, 3rd and 4th integer of each column omitting the pairs containing zero and "energy" lines. My output generated is follows

1 2 1
2 1 2
2 3 2
2 533 2
3 2 3
3 4 3
3 532 3
1 2 5
2 1 6
2 3 6
2 533 6
3 2 7
3 4 7
3 532 7

This code is counting the number of all lines but what i want is that upto 7th row, that is, 3 532 it should print 1 in the 3rd column as the pairs are after the first "energy" occurrence line. Then from 8th row to 14th row i-e from 1 2 to 3 532 it should print 2 in the 3rd column as the pairs are after the second "energy" occurrence. The output should be as follows.

1 2 1
2 1 1
2 3 1
2 533 1
3 2 1
3 4 1
3 532 1
1 2 2
2 1 2
2 3 2
2 533 2
3 2 2
3 4 2
3 532 2

Any help will be appreciated as i am new to programing + perl. Thanks


In reply to Structure number by AG87

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.