few question about array

A

Andrea Spitaleri

Hi I have few quick question about array:
1. reading a file as array, that below code works but I want be sure
that it is correct that portion
I am in doubt about the $numb[$countmol].
open (IN,"in") || die "$!";
@in=<IN>;
clode IN;
my $i;
for ($i=0;$i<$#in;++$i){
chomp $i;
my $countmol=0;
if($in[$i]=~ /^@<TRIPOS>ATOM/){
$initial=$i;
$endmol=$number_atoms + $i;
for($k=$initial;$k<$endmol;++$k){
@coord=split(/ +/,$inmol[$k+1]);
#that is it correct?? $numb[$countmol] is different from $numb[$i].
$numb[$countmol]=$coord[1];
$atom[$countmol]=$coord[2];
$x[$countmol]=$coord[3];
$y[$countmol]=$coord[4];
$z[$countmol]=$coord[5];
$atype[$countmol]=$coord[6];
$atype[$countmol]=$coord[6];
$charge[$countmol]=$coord[9];
++$countmol;
}
}
}
2. If I want create an output like that:
1 2 3
4 5 6
7 8 9
.........
how is the code?

thanks a lot
cheers
and
 
J

Jim Keenan

Andrea said:
Hi I have few quick question about array:
1. reading a file as array, that below code works but I want be sure
that it is correct that portion
I am in doubt about the $numb[$countmol].
open (IN,"in") || die "$!";
@in=<IN>;
clode IN;

Have you read the Posting Guidelines for comp.lang.perl.misc? If you
had, you would have read that before posting a question to the list, you
should have tested your code using the following Perl pragmas:

use strict;
use warnings;

If you had done so, you would have picked up a typing error in the last
line of code above. Before you seek help from others, get as much help
as you can from Perl itself.

Jim Keenan
 
B

Brian McCauley

Andrea said:
open (IN,"in") || die "$!";
@in=<IN>;
clode IN;
my $i;
for ($i=0;$i<$#in;++$i){
chomp $i;
my $countmol=0;
if($in[$i]=~ /^@<TRIPOS>ATOM/){
$initial=$i;
$endmol=$number_atoms + $i;
for($k=$initial;$k<$endmol;++$k){
@coord=split(/ +/,$inmol[$k+1]);
#that is it correct?? $numb[$countmol] is different from $numb[$i].
$numb[$countmol]=$coord[1];
$atom[$countmol]=$coord[2];
$x[$countmol]=$coord[3];
$y[$countmol]=$coord[4];
$z[$countmol]=$coord[5];
$atype[$countmol]=$coord[6];
$atype[$countmol]=$coord[6];
$charge[$countmol]=$coord[9];
++$countmol;
}
}
}
how is the code?

Well, as you ask, awful.

You should always decalare all variables in the smallest applicable
context unless you have a positive reason to do otherwise.

You should not use C-style fors in Perl unless you have a reason to do so.

When programing, you should always ask the machine for as much help as
possible. In the case of Perl you should

use strict;
use warnings;

(Strict disables 3 features of Perl you don't want to use without
knowing what you are doing).

You have a chomp() in your code that makes no sense at all.

Whitespace is not a scarce resource - feel free to use as much as you
like to make your code readable.

It is trivial in Perl to get the length of an array. There is no point
to maintain a separate scalar variable ($countmol) that merely contains
the length of an array. And anyhow there is a special function in Perl,
push(), that appends one or more elements to the end on an array.

It is usually a bad idea to have a data structure that is made up of
many parallel arrays.

It is usually a bad idea to have a data structure that is made up of
many separate named variables.

Rather than writing lots of separate scalar assignments to successive
elements of an array (@coord) it is more ideomatic to use a list assignment.

You should not slurp if you are only going to process the data once, a
line at a time.
 
K

krakle

Hi I have few quick question about array:
1. reading a file as array, that below code works but I want be sure
that it is correct that portion
I am in doubt about the $numb[$countmol].
open (IN,"in") || die "$!";
@in=<IN>;

Declare please.
clode IN;

Yea that seems about right........

.... yadda yadda yadda lots of crappy code ...

Why do you declare only certain variables and not all?
how is the code?

Wonderful. Right, Tad?

Ok.
 
J

Joe Smith

Andrea said:
$initial=$i;
$endmol=$number_atoms + $i;

Error: Undefined variable $number_atoms;

If you had set $number_atoms to a defined value earlier in your
program, you should have included that with your post.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top