CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Storing values in a matrix

 



mamat
Novice

Jan 2, 2010, 5:47 AM

Post #1 of 3 (463 views)
Storing values in a matrix Can't Post

I need some help in printing the values. For a set of co-ordinates I calculate their distances between them (from one point to all other points) with the code below. But I want to store them in a matrix (like an adjacency matrix) and onto a file so that I can do more complex process using a different program. To do this, I am setting the distances into an array of arrays: $AoA[$i1][$i2]. My input:
219.18 275.61
575.58 203.87
623.52 7.44
1109.78 241.48
1245.37 277.37


Code
use warnings; 

use Data::Dumper;

my @points = ();
my $total = 0;
my @AoA;

open(IN, "tw5.txt") or die "$!";
while (my $line = <IN>) {
chomp($line);
my @array = (split (/\s+/, $line));
push @points, [ @array ];
}
close(IN);

print Dumper \@points;

for my $i1 ( 0 .. $#points -1 ){
my ( $x1, $y1 ) = @{ $points[$i1] };
for my $i2 ( 1 .. $#points ){
my ( $x2, $y2 ) = @{ $points[$i2] };
my $dist = sqrt( ($x2-$x1)**2 + ($y2-$y1)**2 );
$AoA[$i1][$i2]=$dist;
print "distance from ( $x1, $y1) to ( $x2, $y2 ) is $dist\n";
print "****$AoA[$i1][$i2] ";
}

}


The output of this code:

Code
363.548604178313 485.18654608305 891.253733175912 1026.19150927105 0 202.1954215 
6043 535.522317088653 673.810725723478 202.19542156043 0 539.651284812702 677.90
8273588691 535.522317088653 539.651284812702 0 140.259545842698

This does not have the desired format i.e. the matrix "shape". Since this forms the input to another program this pattern is desired. How do I achieve this?
Best Wishes,
Mamat


(This post was edited by mamat on Jan 2, 2010, 7:50 PM)


shawnhcorey
Enthusiast


Jan 3, 2010, 5:36 AM

Post #2 of 3 (434 views)
Re: [mamat] Storing values in a matrix [In reply to] Can't Post

Remove the print statements and add this to the end:

Code
for my $row ( @Aoa ){ 
print join( ' ', @$row ), "\n";
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


mamat
Novice

Jan 3, 2010, 6:53 PM

Post #3 of 3 (422 views)
Re: [shawnhcorey] Storing values in a matrix [In reply to] Can't Post

I removed the last print statement and put your code and I get this error: Use of uninitialized value in join or string at calc-dist-orig.pl line 34.

I read in the posts that this is due to null inside the array. I tried to iterate until 0..$#AoA, that gives an error.

Again from the forum,

Code
print join( ' ', map{defined($_)?$_:' '} @$row ), "\n";

. My apologies for putting this without knowing what this actually does. Anyway, it gives the wrong output:

Quote
0 202.19542156043 535.522317088653 673.810725723478
202.19542156043 0 539.651284812702 677.908273588691
535.522317088653 539.651284812702 0 140.259545842698


Many thanks for your time & help.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives