Solved

output problems again!

Posted on 2004-04-16
6
160 Views
Last Modified: 2010-05-18
Hi all, I'm still having a problem with my output, if you can help? My current program is collecting numbers and doing a calculation which is all fine.  However, I have a problem with the spacing in the output file.I'm only dealing with the "NEWFILE" output here in my program.  My current output looks like this:

  1(E)( 95.58)A  2(E)( 24.87)Q  3(E)( 46.58)T  4(E)( 46.25)V..(etc)....10(E)( 23.33)L  11(E)(0.00)I  12(E)( 35.55)K  13(E)(  0.00)A  
14(E)( 35.76)D  15(E)( 44.55)K...(etc)......164(E)( 15.07)T  165(E)(  6.04)I  166(E)( 15.29)G  167(E)( 23.14)Y etc

This is fine but the problem is the spacing between the entries. I currently have 2 spaces between each entry eg 2(E)( 24.87)Q and
3(E)( 46.58)T.  However, because I have 1 digit before the, eg (E), 2 digits and 3 digits the spacing gets mixed up. I need an output format with the same distance between the start of one entry and the start of another. Eg like :1(E)( 95.58)A  2(E)( 24.87)Q if there is one digit before the(E), 10(E)( 23.33)L 11(E)(0.00)I if there are 2 digits before the (E) and like 165(E)(  6.04)I166(E)( 15.29)G if there are 3 digits before the (E).

I think its just a matter of changing the   "printf NEWFILE "$residual($A)(%6.2f)$AA  ", $calcval;" line...I hope you can help!
Thanks






program:

#!/usr/bin/perl
use strict;

my %vals = (A => 113,
            V => 160,
            L => 180,
            I => 182,
            P => 143,
            W => 259,
            F => 218,
            M => 204,
            G => 85,
            S => 122,
            T => 146,
            Y => 229,
            C => 140,
            N => 158,
            Q => 189,
            K => 211,
            R => 241,
            H => 194,
            D => 151,
            E => 183,
            X => "N");

my $offset    = 0;
my $linecnt   = 1;

my @ResVals   = ();
my @AAVals    = ();

open(INFILE, "/home/paul/tester3") or die "cannot open input file $!";
open(OUTFILE, '>b') or die "cannot open output file $!";
open(NEWFILE,'>a')  or die "cannot open output file $!";
while (my $line = <INFILE>) {
     next if ($line =~ /HYDROGEN/);

     my $residual = substr($line, 6,10);
     $residual =~ s/ //g;
     my $AA       = substr($line,13,1);
     $AA =~ s/ //g;
     my $ACC      = substr($line,35,4);
     $ACC =~ s/ //g;
     my $Reset    = substr($line,14,2);  
     my $A=substr($line, 9, 1);
     if ($Reset =~ /\*/) {
           print NEWFILE "\n";
           printf OUTFILE "Surface residues chainA:%s\n" ,  join(",", map{sprintf("%-3s", $_)} @ResVals);
           printf OUTFILE "Surface Resitype chainA:%s\n" ,  join(",", map{sprintf("%-3s", $_)} @AAVals);
           push(@FullResVals,@ResVals);          
           push(@FullAAVals,@AAVals);  

          @ResVals   = ();
          @AAVals    = ();
         
          $offset = $residual;
     }
     my $relres = $residual - $offset;
     
     my $calcval = -1;
     if (defined($vals{$AA})) {
          $calcval = ($ACC / $vals{$AA}) * 100;
     }
     printf NEWFILE "$residual($A)(%6.2f)$AA  ", $calcval;

     
     if ($calcval > 25) {  ## store the values only if the calculated value is greater than 50
          push @ResVals, $relres;
          push @AAVals, $AA;
     }
}
if ($#ResVals >= 0) {
     $pattern=join('|',@FullResVals);
     @ResVals=grep {!/$pattern/} @ResVals;
     printf OUTFILE "Surface residues chainB:%s\n" ,  join(",", map{sprintf("%-3s", $_)} @ResVals);
     printf OUTFILE "Surface resitype  chainB:%s\n" ,  join(",", map{sprintf("%-3s", $_)} @AAVals);
     $pattern=join('|',@FullAAVals);  
     
}

close(INFILE);
close(OUTFILE);
0
Comment
Question by:paulieomeara
  • 4
  • 2
6 Comments
 

Author Comment

by:paulieomeara
ID: 10840620
sorry, that should be an output like this:

1(E)( 95.58)A  2(E)( 24.87)Q if there is one digit before the(E),
10(E)( 23.33)L 11(E)(0.00)I if there are 2 digits before the (E) and like
165(E)(  6.04)166(E)( 15.29)G if there are 3 digits before the (E).
0
 

Author Comment

by:paulieomeara
ID: 10840670
apologies ....for the third: 165(E)(  6.04)I166(E)( 15.29)G if there are 3 digits before the (E).
0
 

Author Comment

by:paulieomeara
ID: 10840736
This is ridiculous I know....but what I need is always 4 spaces altogether between the last letter entry in the example and the start of the open bracket like this for example:
 1(E)( 95.58)A  2(E)( 24.87)Q 10(E)( 23.33)L 11(E)(0.00)I 165(E)(  6.04)F 166(E)( 15.29)G165 (E)(  6.04)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
PerlKing earned 500 total points
ID: 10840765
You want the first number to occupy 3 spaces irrespective of the number of digits it has.

Replace
printf NEWFILE "$residual($A)(%6.2f)$AA  ", $calcval;

with
printf NEWFILE "%3d($A)(%6.2f)$AA  ", $residual, $calcval;

This line should do that trick for you.
(You are telling printf to reserve 3 spaces for $residual irrespective of the number of digits in it)

Check the output with this and let me know if I misunderstood your requirement
0
 
LVL 3

Expert Comment

by:PerlKing
ID: 10840782
Well, if you need 4 spaces and not 3 as was my previous understanding, you could use the following:

printf NEWFILE "%4d($A)(%6.2f)$AA  ", $residual, $calcval;
0
 

Author Comment

by:paulieomeara
ID: 10840869
That greats great..christ, if I'd spotted and solved this problem earlier I'd be a much happier man now!  Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
compress files in RAR using perl 13 79
rename outfile before writing 2 71
Add additional column to .csv using Perl. 8 128
Migrate OTRS to newest version. 1 310
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now