few question about array

Discussion in 'Perl Misc' started by Andrea Spitaleri, Sep 25, 2004.

  1. 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
     
    Andrea Spitaleri, Sep 25, 2004
    #1
    1. Advertising

  2. Andrea Spitaleri

    Jim Keenan Guest

    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
     
    Jim Keenan, Sep 25, 2004
    #2
    1. Advertising

  3. Andrea Spitaleri <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 25, 2004
    #3
  4. 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.
     
    Brian McCauley, Sep 25, 2004
    #4
  5. Andrea Spitaleri

    krakle Guest

    (Andrea Spitaleri) wrote in message news:<>...
    > 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.
     
    krakle, Sep 26, 2004
    #5
  6. Andrea Spitaleri

    Joe Smith Guest

    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
     
    Joe Smith, Sep 27, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murali
    Replies:
    2
    Views:
    611
    Jerry Coffin
    Mar 9, 2006
  2. Chad
    Replies:
    17
    Views:
    638
  3. Replies:
    3
    Views:
    102
    Dr John Stockton
    Aug 3, 2006
  4. Jari Hujanen
    Replies:
    3
    Views:
    128
    Jari Hujanen
    Jan 30, 2009
  5. Replies:
    3
    Views:
    303
Loading...

Share This Page