CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [kapab07] Parsing: Edit Log



Kenosis
User

Mar 26, 2013, 6:01 PM


Views: 778
Re: [kapab07] Parsing

Your script is problematic in a number of areas. The most immediate which guarantees no printing is the following:


Code
if ($l=~/^ATOM/ && $atname=~/N1/){ ...


The variable $atname has not been initialized anywhere, so the overall expression will always evaluate to false.

Here are some other items to keep in mind:

Always:

Code
use strict; 
use warnings;


at the top of your script. Use the three-argument for of open, i.e.:


Code
open my $fh, '<', $ARGV[0] or die $!;


The above also demonstrates using lexical file handles and handling open failures.

The following:


Code
$file = <FILE>;


will only read the first line of the file into the variable $file . Then, you have $file as the iterable in your foreach loop, but you should have the file handle there.

Finally, you don't need exit at the end of your script, as it will automatically exit when finished.

Since you're sending the script a file from the command line, you can do the following:


Code
#!/usr/bin/env perl 
use strict;
use warnings;

while (<>) {
my $atname = substr( $_, 66, 1 );

if ( /^ATOM/ and $atname =~ /N1/ ) {
my $X = substr( $_, 27, 6 );
my $Y = substr( $_, 33, 6 );
my $Z = substr( $_, 42, 6 );
print "$atname, $X, $Y, $Z\n";
}
}


This lets Perl open and close the file. Each of the file's lines are assigned to $_, the default scalar.

I don't have your dataset, but I believe you intended to say the above.

Hope this helps!


(This post was edited by Kenosis on Mar 26, 2013, 6:04 PM)


Edit Log:
Post edited by Kenosis (User) on Mar 26, 2013, 6:02 PM
Post edited by Kenosis (User) on Mar 26, 2013, 6:02 PM
Post edited by Kenosis (User) on Mar 26, 2013, 6:03 PM
Post edited by Kenosis (User) on Mar 26, 2013, 6:04 PM
Post edited by Kenosis (User) on Mar 26, 2013, 6:04 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives