in reply to 'my' headache...why doesn't this work?

For those wondering if I am losing the variable outside its 'my' scope: I am printing the result immediately after the calculations, like:

my $columnsum += $_->[$colnum] for @data; print "Total value = $columnsum";

...and...

$columnsum = 0; $columnsum += $_->[$colnum] for @data; print "Total value = $columnsum";

======================
Sean Shrum
http://www.shrum.net

Replies are listed 'Best First'.
Re: Re: 'my' headache...why doesn't this work?
by tachyon (Chancellor) on Apr 05, 2002 at 10:20 UTC
    # your code my $columnsum += $_->[$colnum] for @data; # is effectively the same as for (@data) { my $columnsum += $_->[$colnum] } # so as expected $columnsum is effectively localised to that block (li +ne). my $i += $_ for 1..3; print "localised (1) \$i='$i'\n"; for (1..3) { my $i += $_ } print "localised (2) \$i='$i'\n"; $i += $_ for 1..3; print "not localised (1) \$i='$i'\n"; for (1..3) { $i += $_ } print "not localised (2) \$i='$i'\n"; print "Oops, forgot to zero a widely scoped var!";

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print