Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Use perl to do direction matching

by hujunsimon (Sexton)
on Oct 09, 2009 at 14:56 UTC ( #800299=perlquestion: print w/replies, xml ) Need Help??
hujunsimon has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,

i'm working on a problem involving using the gps data to figure out the direction of each link (i.e. northwest, south east...). i wrote a code as below, but it keeps giving me error, about "global symbol $A requires explicit package name...." anyone can help ?

#!/usr/bin/perl use strict; use warnings; # 1)input file my $file = "c:/documents and settings/desktop/dirTable.csv"; open (TEMP, $file) or die("Error: cannot open $file\n"); # 2)output file my $outfile = "c:/documents and settings/desktop/dirMatched.csv"; open (OUTPUT,">$outfile") or die("Error: cannot open $outfile\n"); #Private variables my $nline = "\n"; my $comma = ","; my @direction = ["east","west","north","south"]; #Main body while (my $line = <TEMP>) { my @column = split (/,/,$line); if ($column[4] < $column[7]) { my $A = $direction[0]; }else {$A = $direction[1]; } if ($column[5] < $column[8]) { my $B = $direction[2]; }else {$B = $direction[3]; } $dir = $A.$B; print OUTPUT $column[4],$comma,$column[5],$comma,$column[7],$comm +a,$column[8],$comma,$A,$comma,$B,$comma,$nline; }

$column4,7 are the x coordinates. $column5,7 are the y coordinates.

thanks a lot,

Replies are listed 'Best First'.
Re: Use perl to do direction matching
by moritz (Cardinal) on Oct 09, 2009 at 14:58 UTC
    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.

      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.
Re: Use perl to do direction matching
by ikegami (Pope) on Oct 09, 2009 at 15:27 UTC
    my @column = split (/,/,$line); my $A = $column[4] < $column[7] ? $direction[0] : $direction[1]; my $B = $column[5] < $column[8] ? $direction[2] : $direction[3];

    But use meaningful names!

    my ($at_x, $at_y, $goal_x, $goal_y) = ( split(/,/, $line) )[4,5,7,8]; my $x_dir = $at_x < $goal_x ? 'East' : 'West'; my $y_dir = $at_y < $goal_y ? 'North' : 'South';

      it works, thanks a lot guys !

Re: Use perl to do direction matching
by vitoco (Friar) on Oct 09, 2009 at 18:03 UTC

    Instead of:

    my $nline = "\n"; my $comma = ","; ... print OUTPUT $column[4],$comma,$column[5],$comma,$column[7],$comm +a,$column[8],$comma,$A,$comma,$B,$comma,$nline;


    # $/ = "\n" # Input RS already defined $, = ","; ... print OUTPUT $column[4],$column[5],$column[7],$column[8],$A,$B; print $/; # or use 'say' as it was said.

    or interpolate:

    print OUTPUT "$column[4],$column[5],$column[7],$column[8],$A,$B\n +";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://800299]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2016-10-25 17:15 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (326 votes). Check out past polls.