Boon Chong Ang <mailto:[EMAIL PROTECTED]> wrote:

: Hi,
: I am having problem to automatic feed in the data and solve
: the equation. Well, datatest.txt contains the data that I
: need to manipulate, the script is testing_abc1.pl and
: Matrix.pm is the one that I download from the net and use it
: at the local directory since I don't have the installation
: right. I would appreciate that some one would give me work
: around solutions or approach.


    You really should begin again. You're are not using
strict. The use of 'foreach' over 'while' and of indenting
would improve the code readability and you need to pick more
descriptive names for variables.

    I know this is a pain, but you are trying to walk without
ever having crawled. Let's take a look at some examples.
Here's a snippet you provided.

my $infile =$ARGV[0];
open (IN, "$infile") or die $!;
$outfile= "check.val";
open (OUT, "> $outfile") || die $!;

$count=0;
while (<IN>){
@mo=split(/\s+/,$_);
push @da,$mo[0];
push @db,$mo[1];
push @dc,$mo[2];
$count++;
$total =$count
}
close(IN);

    $outfile is not a lexical variable. Why leave 'my' off
here and use it on other variables? What are the rules you
use to determine which variables are lexical and which will
reside in the symbol table? The same questions need to be
asked for $count, @mo, @da, @db, @dc, and $total.

    Why open the output file now? Why not wait until after
you closed the input file? Nothing is done with the output
file until then.

    What is $count for? In fact, what is the point of $total?
The scalar value of @da gives the same value.

    What does 'mo' mean? I assume English isn't your first
language, but can't you find something more meaningful than
'@mo', @da, @db, and @dc?

    Use indentation when writing a loop and don't squash
all your white space together:

my $file = $ARGV[0] || die usage();
open FH, $file or die qq(Cannot open "$file": $!);

    my( @da, @db, @dc );
    while ( <FH> ) {
        my @mo = split /\s+/;
        push @da, $mo[0];
        push @db, $mo[1];
        push @dc, $mo[2];
    }

close FH;


$file= 'check.val';
open FH, "> $file" or die qq(Cannot open "$file": $!);

    my $max     = 4;
    my $dif     = @da - $max;
    my $total   = @da;

    printf FH " dif %s max %s total %s\n", $dif, $max, @da;
    printf FH "total = %s\n", @da;

    foreach my $num ( 0 .. $#da ) {
        print FH "$da[$num], $db[$num], $dc[$num]\n";
        $num++;
    }

close FH;

   Consider using 'foreach' in place of 'while' when appropriate.
Calculate math down to avoid repeated calculations. Here is
the code you provided.

while($dset ne $dif){
$inc=0;
$max=4;
$inc=$inc+$dset;
$max=$max+$dset;
while ($inc ne $max){
my $w=0;
while ($w le 1){
my $x=0;
while ($x le 1){
$a[$dset][$inc-$dset][$w][$x]=($da[$inc]**$x)*($db[$inc]**$w);
$x++;
}
$w++;
}
$inc++;
}
$dset++;
}

    It might be written as:

my @a;
foreach my $dset ( 0 .. $#da - $max ) {
    foreach my $i ( $dset .. $dset + 3 ) {
        push @{ $a[$dset] }, [
            [       1,           $da[$i], ],
            [ $db[$i], $db[$i] * $da[$i], ],
        ];
    }
}

    Notice the use of white space in organizing the
algorithm. It also aids in readability. The variables are
now lexically scoped and we have eliminated $x and $w
completely.

    Start your program again. Use this at the beginning
of the script.

#!/apps/perl/bin/perl -w

use strict;


    Add a small amount of code and check for errors. Once
everything looks good, write a little more and then check
for errors. Keep writing that way until you get what you
want.

    If you get stuck on some part ask us about just that
part. Be specific when you ask your question. Tell us what
you expect and tell us what you are getting instead.

    Pretend that we don't have the time required to
download all your files and look them to find what the
problem might be and how to solve it.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328



















-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to