AG87 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Structure number
by CountZero (Bishop) on Dec 01, 2010 at 07:29 UTC
    use Modern::Perl; my $sequence; while (<DATA>) { if (/ENERGY/) { $sequence++; next; } my ( $first, @fields ) = grep { $_ } split; say "$first $_ $sequence" for @fields; } __DATA__ 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

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thanks

        Isn't some modification possible in my code/logic to have the desired output? Actually columns and rows are being added up in the file. and the i cant modify the "grep" condition accordingly. For example i want the same output for the file 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

        here the second and the sixth column is added. I have been able to omit the 2nd column but how can i omit the 6th column? The code is

        open(MYFILE, $data_file) || die("Could not open file!"); { 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 "$key $field $energy\n"; } } } } close(MYFILE);

        your help will really be appreciated. Thanks

Re: Structure number
by jwkrahn (Abbot) on Dec 01, 2010 at 07:18 UTC

    Updated, thanks CountZero.

    use warnings; use atrict; open MYFILE, '<', $data_file or die "Could not open '$data_file' because $!"; my $energy; while ( <MYFILE> ) { if ( /\bENERGY\b/ ) { ++$energy; next; } my ( $key, @fields ) = grep $_, split; for my $field ( @fields ) { print "$key $field $energy\n"; } }

      Thank you so much for help. It worked.

        Will you please kindly tell me what will be the modification if another column is added to the input file? but the output should be the same as the previous one omitting the alphabets? If the input file is

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

        I have tried the following but its not working

        open(MYFILE, $data_file) || die("Could not open file!"); my $energy; while ( <MYFILE> ) { if ( /\bENERGY\b/ ) { ++$energy; next; } my ( $key, @fields ) = grep $_, split; for my $field ( @fields ) { if (@fields[2] ne G || A || C || G) { print "$key $field $energy\n"; } } }