Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: incrementing already existing file by broomduster
in thread incrementing already existing file by wanttoprogram

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-18 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found