Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > few question about array

Reply
Thread Tools

few question about array

 
 
Andrea Spitaleri
Guest
Posts: n/a
 
      09-25-2004
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
 
Reply With Quote
 
 
 
 
Jim Keenan
Guest
Posts: n/a
 
      09-25-2004
Andrea Spitaleri wrote:
> 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
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-25-2004
Andrea Spitaleri <(E-Mail Removed)> wrote:

> clode IN;



Please be more respectful of the thousands of people that
you are writing to.

Post your _real_ Perl code please.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-25-2004


Andrea Spitaleri wrote:

> 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.

 
Reply With Quote
 
krakle
Guest
Posts: n/a
 
      09-26-2004
(E-Mail Removed) (Andrea Spitaleri) wrote in message news:<(E-Mail Removed). com>...
> 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?

> and


Ok.
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      09-27-2004
Andrea Spitaleri wrote:

> $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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Internet connects for a few seconds then disconnects for a few sec Cody Wireless Networking 2 06-28-2009 08:24 PM
Problem with array only accepting a few rows or elements Jari Hujanen Javascript 3 01-30-2009 01:38 PM
How do I search a extract only a few elements in a huge JS array mdh_2972@hotmail.com Javascript 3 08-03-2006 01:31 PM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM



Advertisments