Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Issues regarding for loops and recursion

by choroba (Cardinal)
on Jan 09, 2023 at 12:44 UTC ( #11149455=note: print w/replies, xml ) Need Help??


in reply to Issues regarding for loops and recursion

You are missing a \s+ between a \w+ and \d+. Also, the whole part needs to be moved inside the loop.

#! /usr/bin/perl use warnings; use strict; open my $in, '<', '6U9D.pdb' or die $!; my %amino_acid_conversion = (ALA => 'A', TYR => 'Y', MET => 'M', LEU = +> 'L', CYS => 'C', GLY => 'G', ARG => 'R', ASN = +> 'N', ASP => 'D', GLN => 'Q', GLU => 'E', HIS = +> 'H', TRP => 'W', LYS => 'K', PHE => 'F', PRO = +> 'P', SER => 'S', THR => 'T', ILE => 'I', VAL = +> 'V'); my ($x, $y, $z) = (0, 0, 0); my $seq; while (<$in>) { if (/HEADER\s+(.*)/) { print ">$1\n"; } if (/^SEQRES\s+\d+\s+\w+\s+\d+\s+(.*)/) { $seq .= $1; } if (/^ATOM\s+\d+\s+\w+\s+\w+\s+\w+\s+\d+\s+(\S+)\s+(\S+)\s+(\S+)/) + { # ~~~ $x += $1; $y += $2; $z += $3; } } $seq =~ s/ //g; my $k = 0; for (my $i = 0; $i < length $seq; $i += 3) { my $triplet = substr $seq, $i, 3; if (exists $amino_acid_conversion{$triplet}) { print "$amino_acid_conversion{$triplet}"; } else { warn "Don't know how to convert '$triplet'.\n"; } $k++; } print "\n"; my $xgk = $x / $k; my $ygk = $y / $k; my $zgk = $z / $k; print "$xgk $ygk $zgk \n";

Note that I also turned strict and warnings on, used the 3-argument version of open without bareword filehandles, and checked its return value.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2023-09-30 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?