David wrote:
My simple program works well in the terminal window:

#!/usr/bin/perl -w
use strict;
my $answer;

for ($a = 1; $a <= 100; $a++)
{
    for ($b = 1; $b <= 100; $b++)
        {
        $answer = ($a-$b);
          print "$a - $b\t$answer\n";
        }
}


Output:
1 - 1   0
1 - 2   -1
1 - 3   -2
1 - 4   -3
1 - 5   -4

[etc...]

However, when I insert some code to write to a file it fails an odd death:

#!/usr/bin/perl -w
use strict;
my $answer;
open(WRITE,"+>/Users/dave/Documents/Programming/Perl/081008mathtables/add.txt")

You are missing a semicolon (;) at the end of that statement. And you should *always* verify that the file opened correctly:

open WRITE, '>', '/Users/dave/Documents/Programming/Perl/081008mathtables/add.txt' or die "Cannot open '/Users/dave/Documents/Programming/Perl/081008mathtables/add.txt' $!";


for ($a = 1; $a <= 100; $a++)
{
    for ($b = 1; $b <= 100; $b++)
        {

You shouldn't use $main::a and $main::b outside of a sort block. That is usually written as:

for my $a ( 1 .. 100 )
{
    for my $b ( 1 .. 100 ) {
    {


        $answer = ($a-$b);

With strict enabled you have to declare variables:

        my $answer = $a - $b;


          print WRITE "$a - $b\t$answer\n";

        }
}

close(WRITE);



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to