I have a little bit problem in my code which I am unable to sort out. My input file is

536 ENERGY = -176.2 pro
1 G 0 2 0 1
2 G 1 3 533 2
3 G 2 4 532 3
536 ENERGY = -175.9 pro
1 G 0 2 0 1
2 G 1 3 533 2
3 G 2 4 532 3

My code is

$data_file="HIVgag.ct"; open(MYFILE, $data_file) || die("Could not open file!"); $b=$between=$energy=0; while (<MYFILE>) { if (/energy/i) { $energy++; $between=$b; } elsif ($energy) {$b++} } print "$energy / $between\n"; close(MYFILE); open(MYFILE, $data_file) || die("Could not open file!"); open(WRITE, ">final.net"); print WRITE "*vertices $between\n"; while(<MYFILE>) { foreach $row (<MYFILE>) { @row = $row; (@row[0],@row[1],@row[2], @row[3], @row[4], @row[5], @row[6])=split(/\ +s+/,$row); if (@row[2] ne ENERGY) { print WRITE "@row[1] $row[2]\n"; print"@row[1] $row[2]\n"; } } } close(MYFILE); open(MYFILE, $data_file) || die("Could not open file!"); { print WRITE "*edges\n"; my $energy; while ( <MYFILE> ) { if ( /\bENERGY\b/ ) { ++$energy; next; } my ( $key, @fields ) = grep $_, split; for my $field ( @fields ) { if ( $field ne 'G' && $field ne 'A' && $field ne 'C' && $field ne +'G' ) { print WRITE "$key $field $energy\n"; } } } } close(WRITE);

My code rearranges the input file in a way such that the first two columns of the file is written in *vertices portion. And in the *edges portion the 1st column of the input file makes a pair with the 3rd, 4th , 5th and 6th column omitting the pairs containing zero in 3rd, 4th, 5th and 6th column. The 3rd column of *edges consists of the number of “energy block” in which the pair exists. Thus the output file is

*vertices 3
1 G
2 G
3 G
1 G
2 G
3 G
*edges
1 2 1
1 1 1
2 1 1
2 3 1
2 533 1
2 2 1
3 2 1
3 4 1
3 532 1
3 3 1
1 2 2
1 1 2
2 1 2
2 3 2
2 533 2
2 2 2
3 2 2
3 4 2
3 532 2
3 3 2

What I want is in the *vertices portion it should only read the first two columns of the first “energy block” and then it should jump to *edges portion which implies that in this case it should only print 1 G, 2 G and 3 G and then jump to *edges portion. And in *edges portion the first column of input file should not make pair with 6th (last) column. So the output should be like

*vertices 3
1 G
2 G
3 G
*edges
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

I shall be very thankful for help.


In reply to Spliting file + removing column 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.