wanttoprogram has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; #crappy open (MYFILE, "2hgs_d00_internal_nrg_e.dat"); our $nrgval = " "; our $chn = " "; our $count; #our $chn[$count]; our $count2; #our $chn2[$count2] our @nrg; open (NEWF, "2HGS_bio_conv-min_p.pdb"); our $toprint = " "; our $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 our $j = 0; our $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);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: incrementing already existing file
by roboticus (Chancellor) on Feb 28, 2011 at 00:42 UTC | |
If you keep your indentation and other whitespace consistent and clean, it's easier to see errors in your code. Additionally, I like to declare variables where I need them, and I don't create variables I don't use. ;^) (In other words, I deleted a few variables that you weren't using.) So I altered your code a bit, like this:
Having done that, it's a bit easier to see why you get only one value from MYFILE. You read from it in the outermost loop, and then process the entire NEWF file. Then, when it's time to read the second record from MYFILE, your NEWF is empty, so it completely skips the inner loop from then on. Generally, if your code just creeps rightward like this, it's indicative of a problem of some sort. I'm normally uncomfortable with more than, say, four levels of indentation. Beyond that, I tend to either change my logic, or pull out some subroutines to simplify things. One last thing: You have some strange loops in the form:
You know that the loop should execute only one time, right? I would normally assume you meant something else and just keyed in the wrong thing. But since you have it repeated I thought I'd point it out to you. For example, try this program out:
You should review how loops work, and then change the logic to do more of what you want. If you're wanting to work through both files in parallel, you might want to check out the logic in Re: How to deal with Huge data and/or Re: parallel reading. ...roboticus When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] [select] |
by wanttoprogram (Novice) on Feb 28, 2011 at 02:00 UTC | |
| [reply] [d/l] |
by broomduster (Priest) on Feb 28, 2011 at 03:20 UTC | |
Note that I use split (not substr) to get the fields of interest from each line (same approach for both files). For the output, I join the fields with a tab character. You should change that to something else (e.g., a fixed number of space characters) if you need the output formatted differently. And of course this writes to STDOUT, so you will need to redirect the output on the command line or add to this code to open an output file and print to that. When you are more comfortable with Perl, you will find that some of this is actually on the "verbose" side. Using Perl idioms would make some of my code more compact, but also a bit harder to follow until you have more experience. | [reply] [d/l] |
by wanttoprogram (Novice) on Feb 28, 2011 at 22:57 UTC | |
by broomduster (Priest) on Mar 01, 2011 at 00:04 UTC | |
| |
by roboticus (Chancellor) on Feb 28, 2011 at 02:57 UTC | |
OK, are both files sorted with respect to the key fields? If so, then you don't really want nested loops. You want a single loop and you can decide which file to read depending on what the current condition is. Something like:
Of course, if either of the files aren't sorted on the keys, then that won't work. You'll either have to sort them, or try something like a hash table. For the hash table, you simply read the first file into a hash based on the key field(s). Then you scan through the second file, looking up values from the hash as you need them. Something like:
...roboticus When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] [select] |
Re: incrementing already existing file
by broomduster (Priest) on Feb 28, 2011 at 00:31 UTC | |
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): 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:
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. | [reply] [d/l] [select] |
by wanttoprogram (Novice) on Feb 28, 2011 at 00:51 UTC | |
| [reply] [d/l] |
by broomduster (Priest) on Feb 28, 2011 at 01:06 UTC | |
| [reply] |
by umasuresh (Hermit) on Feb 28, 2011 at 13:55 UTC | |
| [reply] |
by wanttoprogram (Novice) on Feb 28, 2011 at 22:58 UTC |