Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^2: perl error

by topaz (Initiate)
on Jul 28, 2011 at 23:50 UTC ( #917367=note: print w/replies, xml ) Need Help??

in reply to Re: perl error
in thread perl error

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; }

Replies are listed 'Best First'.
Re^3: perl error
by dreadpiratepeter (Priest) on Jul 29, 2011 at 00:10 UTC
    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.

    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
      Many thanks for your advice :) I will try my best
Re^3: perl error
by Anonymous Monk on Jul 28, 2011 at 23:58 UTC
      He definitely wants $row[$a+180] rather than $row($a+180) in the matrix operation, but I really think he wants just $a+180, since he never fills @row and @column with anything other than 0.

      "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
      Yes ofcourse, sorry I missed that :s Many thanks for your help :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://917367]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2018-07-23 04:20 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (459 votes). Check out past polls.