Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: incrementing already existing file

by broomduster (Priest)
on Feb 28, 2011 at 00:31 UTC ( #890481=note: print w/ replies, xml ) Need Help??


in reply to incrementing already existing file

You gave a nice abstract description of what you want to accomplish (in terms of substrings from files). We really need a more concrete description that includes short samples of each of the files (just a few lines from each). Explain what you want to happen, e.g., read a line from file1, do thus-and-so to it; then read a line from file2 and do something to it; then make such-and-such comparison; then print (or not) depending on the result. Chances are that once you explain it here well enough for us to understand, you will have a better idea of how your code is falling short of your expectations.

As for the code you posted, here are some general comments, but they won't fix the problem(s) you tried to describe in your post.

Firstly, I reformatted your code (nicer indentation, eliminate lots of extra white space) to make it more readable. Here it is (see below for some specific comments):

#!/usr/bin/perl -w use strict; #crappy open (MYFILE, "2hgs_d00_internal_nrg_e.dat"); my $nrgval = " "; my $chn = " "; my $count; #my $chn[$count]; my $count2; #my $chn2[$count2] my @nrg; open (NEWF, "2HGS_bio_conv-min_p.pdb"); my $toprint = " "; my $chn2 = " "; while (<MYFILE>) { chomp; # avoid \n at the end of each line if ($_ =~/ENERGY/) { for ($count=1;$count<=1;$count++) { $chn = substr $_, 20, 3; $nrgval = substr $_, 35, 8; while (<NEWF>) { chomp; # avoid \n at the end of each line my $j = 0; my $i = 0; if ($_ =~/ATOM/) { for ($count2=1;$count2<=1;$count2++) { $chn2 = substr $_, 23, 3; $toprint = substr $_, 0, 65; for ($chn=1;$chn<=$chn2;$chn++) { if ($chn==$chn2) { print " $toprint $nrgval \n"; } } } } } } } } close (MYFILE); close (NEWF);
Comments:
I changed all of your uses of 'our' to 'my'. They are not interchangeable, and the details of when you really want 'our' are probably more than you need to worry about right now. Until you get more experience, stick to 'my'. Other than those changes and reformatting, the code is yours, but here are some things you should do:
  • Re-read the documentation for open. You need to specify whether files are for reading, writing, or appending. You got the behavior you wanted, but you should get in the habit of saying explicitly what you want.
  • Get in the habit of checking whether open succeeded:
    open( .... ) or die "could not open file: $!";
  • You will want to learn to use Perl's for / foreach rather than the C-style for loops that you have here. Once it's clear what you're trying to do, it will also be clear how to make things more Perl-ish. (I'm sure you are aware that two of your loops make only one pass, but I suspect you did that intentionally for debugging purposes.)
  • You have these two lines (commented out):
    #my $chn[$count]; #my $chn2[$count2]
    If you really need arrays, they would be declared like so:
    my( @chn, @chn2 );
    We'll know if you need them when we see some data and a description of what should happen.

Those are comments about good programming practice. None of them are likely to solve your problem. Post some sample data for the two files. Include a description of what you want your code to do (both in terms of program steps and what you want the output to look like). If, in the process of doing that you get it working on your own, so much the better. If you're still having problems, at least then we have some data to test help us help you solve your remaining problems.


Comment on Re: incrementing already existing file
Select or Download Code
Re^2: incrementing already existing file
by wanttoprogram (Novice) on Feb 28, 2011 at 00:51 UTC
    <MYFILE> has columns similar to this. I want to save column # and last column value when I open this file.I tried to save in sub-strings
    IN CHAIN A RESIDUE 1 HAS ENERGY 28.8216 IN CHAIN A RESIDUE 2 HAS ENERGY 24.9274 IN CHAIN A RESIDUE 3 HAS ENERGY 19.0884 IN CHAIN A RESIDUE 4 HAS ENERGY -27.6978 IN CHAIN A RESIDUE 5 HAS ENERGY 34.8558 IN CHAIN A RESIDUE 6 HAS ENERGY 17.9725 IN CHAIN A RESIDUE 7 HAS ENERGY 29.0379 IN CHAIN A RESIDUE 8 HAS ENERGY 13.7192 IN CHAIN A RESIDUE 9 HAS ENERGY 15.3481 IN CHAIN A RESIDUE 10 HAS ENERGY -7.55393 IN CHAIN A RESIDUE 11 HAS ENERGY -5.87837 IN CHAIN A RESIDUE 12 HAS ENERGY 40.7543 IN CHAIN A RESIDUE 13 HAS ENERGY -11.5488 IN CHAIN A RESIDUE 14 HAS ENERGY -11.7673 <NEWF> has columns similar to this. I want to save column # and compar +e it with previous MYFILE vale. When they match I want to print the valve that I stored in previous fi +le in place of last but one column (bold column) ATOM 1 N MET 1 4.440 25.987 -14.585 1.00 0.00 + A ATOM 2 HT1 MET 1 4.524 25.860 -15.614 1.00 0.00 + A ATOM 3 HT2 MET 1 4.109 26.952 -14.383 1.00 0.00 + A ATOM 4 HT3 MET 1 3.729 25.316 -14.228 1.00 0.00 + A ATOM 5 CA MET 1 5.708 25.747 -13.831 1.00 0.00 + A ATOM 6 HA MET 1 6.008 24.725 -14.011 1.00 0.00 + A ATOM 7 CB MET 1 6.792 26.728 -14.367 1.00 0.00 + A ATOM 8 HB1 MET 1 6.812 26.659 -15.475 1.00 0.00 + A ATOM 9 HB2 MET 1 6.502 27.769 -14.109 1.00 0.00 + A ATOM 10 CG MET 1 8.241 26.517 -13.880 1.00 0.00 + A ATOM 11 HG1 MET 1 8.298 26.709 -12.787 1.00 0.00 + A ATOM 12 HG2 MET 1 8.547 25.462 -14.049 1.00 0.00 + A ATOM 20 N ALA 2 6.330 25.384 -11.469 1.00 0.00 + A ATOM 21 HN ALA 2 7.105 24.825 -11.751 1.00 0.00 + A ATOM 22 CA ALA 2 6.383 25.717 -10.067 1.00 0.00 + A ATOM 23 HA ALA 2 6.344 26.791 -9.955 1.00 0.00 + A ATOM 24 CB ALA 2 5.300 25.034 -9.205 1.00 0.00 + A ATOM 25 HB1 ALA 2 4.288 25.319 -9.565 1.00 0.00 + A ATOM 26 HB2 ALA 2 5.394 23.928 -9.255 1.00 0.00 + A ATOM 27 HB3 ALA 2 5.396 25.346 -8.143 1.00 0.00 + A ATOM 28 C ALA 2 7.753 25.238 -9.659 1.00 0.00 + A ATOM 29 O ALA 2 8.299 24.357 -10.317 1.00 0.00 + A ATOM 30 N THR 3 8.353 25.813 -8.605 1.00 86.23 + A ATOM 31 HN THR 3 7.908 26.533 -8.079 1.00 0.00 + A ATOM 32 CA THR 3 9.687 25.408 -8.176 1.00 88.87 + A ATOM 33 HA THR 3 9.829 24.356 -8.373 1.00 0.00 + A ATOM 34 CB THR 3 10.847 26.194 -8.810 1.00 91.62 + A ATOM 35 HB THR 3 11.790 25.982 -8.261 1.00 0.00 + A
      OK so far, I think. But you need to be a bit clearer.
      • When you say "column #" what do you mean exactly?
      • Given the first few lines of each file (say five lines), what should the output look like? Not just a description, but please show the actual output you want to create.
      It looks like you are trying to add energy value of each residue in one file to a PDB file, is this what you are trying to accomplish?
        That's right

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://890481]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-11-28 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls