Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

perl error

by topaz (Initiate)
on Jul 28, 2011 at 22:46 UTC ( #917359=perlquestion: print w/ replies, xml ) Need Help??
topaz has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, Could you please help me understand this error :s
>Scalar value @Matrix[$a] better written as $Matrix[$a] at ./test.pl l +ine 22. syntax error at ./test.pl line 22, near "][" Execution of ./test.pl aborted due to compilation errors.
------------------------------ from this code
use strict; #-- Define in/out files my $inf = "test.txt" ; #-- variables and handles my $line; my $a; my $b; my @result; my @Matrix = ( [(0) x 361], [(0) x 361]); #--Open the input and output files open (IN, "$inf") || die "Can't read $inf"; while ($line = <IN>) { @result = split(/\s+/, $line); $a = $result[0]; $a = int($phi); $b = $result[1]; $b = int($psi); @Matrix[($a+180)][($b+180)] += 1; }

Comment on perl error
Select or Download Code
Re: perl error
by dreadpiratepeter (Priest) on Jul 28, 2011 at 22:57 UTC
    In the line in question, you are dealing with one element of the array, which is a scalar, so you use $ not @. To demonstrate with a simpler example, @a is an array. $a[0] is the first element in the array.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
      you mean i should do some thing like this ? :s
      use strict; #-- Define in/out files my $inf = "test.txt" ; #-- variables and handles my $line; my $a; my $b; my @result; @row = ((0) x 361); @column = ((0) x 361); my @Matrix = ( @row, @column); #--Open the input and output files open (IN, "$inf") || die "Can't read $inf"; while ($line = <IN>) { @result = split(/\s+/, $line); $a = $result[0]; $a = int($phi); $b = $result[1]; $b = int($psi); @Matrix[$row[($a+180)]][$column[($b+180)]] += 1; }
        Actually, your code has a number of problems.
        • that isn't how you initialize a 2-dimensional array. your initialization of @matrix will make a 722 element 1d array. You would need something like: @matrix=map {[(0) x 361]} (0..361);
        • From what I can see you want:  $Matrix[$a+180][$b+180] += 1;

        I would suggest giving Modern Perl or Learning Perl and Programming Perl a good read to get down some of the fundamentals.

        Update: Also you set $a and $b to values, then immediately set them to int($phi) and int($psi) which are undefined. If you were running with strict and warnings the compiler would have shown you that.



        -pete
        "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: perl error
by toolic (Bishop) on Jul 29, 2011 at 00:44 UTC
Re: perl error
by Tanktalus (Canon) on Jul 29, 2011 at 04:51 UTC

    I know this isn't part of your question, but I also want to add that you should read perldoc perlvar, specifically the part on special variables $a and $b. Long story short, don't use variables with those names, they're reserved. Besides, they're horrible names. $x and $y are less horrible only because they have standard mathematical meanings.

    Thus, your code should be more like this:

    use strict; use warnings; # important, too. #-- variables and handles my @Matrix = map { [ (0) x 361 ] } 0..360; #--Open the input and output files # use 3-arg open, use lexical filehandle, don't interpolate a variable # that doesn't need anything added to it, i.e., "$inf" is bad, but "$i +nf-foo" # is not, report WHY open fails, not merely that it has failed. open my $in, '<', $foo or die "Can't read $inf: $!"; # lexical $line with minimal scope, check for definedness # (if you have a line that is "0\n", you'll find your original code # will terminate early). while (defined (my $line = <IN>)) { # usually, split ' ', $line is what you want instead of /\s+/. # but they do slightly different things, so I'm not changing it. # Added "my" so we create a new @result each time. my @result = split(/\s+/, $line); # I'm guessing here since I don't know where $phi and $psi really # come from. my $phi = int($result[0]); my $psi = int($result[1]); # use $ instead of @ to signify we want a scalar value from # the array. Use ++ instead of += 1 because it's more idiomatic # though technically it's no big deal either way. $Matrix[$phi+180][$psi+180] ++; }
    Note that you're going from angle 0 through 360, which means that you're duplicating the ends. Normally, I'd think you'd want just 0..359 - because 360 and 0 are the same angle.

Re: perl error
by cdarke (Prior) on Jul 29, 2011 at 07:53 UTC
    The prefix character (also known as a sigil) for an element of an array (and hash) is often confusing for new-comers to Perl. It does not indicate the type of the variable name which follows it, but the type of the whole expression.
    For example, take:
    my @Matrix; $Matrix[$a] = 1;
    Perl knows that 'Matrix' is an array because of the [ ] which follows the name (just as if it was a hash it would have { } following). The $ indicates the context of what is inside the [ ], in this case a scalar. Using @ would indicate that a list of indexes is inside the [ ], which is perfectly legal, and generally known as a slice.

    The above only applies to Perl 5, Perl 6 is another country (but on the same continent).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (19)
As of 2015-07-06 20:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls