in reply to Use perl to do direction matching

The my $A makes $A only visible inside the block where it's declared, but you're trying to access it from the outside.

So use something along these lines:

while (my $line = <TEMP>) { my $A; my @column = split (/,/,$line); ... }

But $A is a horrible name for a variable, consider picking a better one.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: Use perl to do direction matching
by hujunsimon (Sexton) on Oct 09, 2009 at 15:18 UTC

    After i declare the variable as you suggested, it comes out another error, said 'use of uninitialized value $A in concatenation <.>.....'

    #Main body while (my $line = <TEMP>) { my $A; my $B; my @column = split (/,/,$line); if ($column[4] < $column[7]) { $A = $direction[0]; }else {$A = $direction[1]; } if ($column[5] < $column[8]) { $B = $direction[2]; }else {$B = $direction[3]; } my $dir = $A.$B; print OUTPUT $column[4],$comma,$column[5],$comma,$column[7],$comm +a,$column[8],$comma,$dir,$comma,$nline; }

      It's not an error, it's a warning. So you have to find out why it's undef. Check the code where you assign to it, find out which branch it took, why the value you assign to it is undef etc.

      You know that $A is either $direction[0] or $direction[1], so try to print those:

      use 5.010; # enable say(), which is shorter than print ..., "\n"; ... say "Direction 0: '$direction[0]'"; say "Direction 1: '$direction[1]'";

      That should give you some clue of what's wrong. You can also use Data::Dumper for debugging.

      Oh, and you still haven't chosen a better name for $A - you really should. Since Perl doesn't know what your code means (at least not in the way you think it should), you should help us understand what it should mean - by using a good names, for example.

      For fixing it, I recommend reading the section List value constructors in perldata, or at perlintro.

      Perl 6 - links to (nearly) everything that is Perl 6.