doozy has asked for the wisdom of the Perl Monks concerning the following question:

I have a code that determines the distance between some points and lists the distances in a matrix like so,

0 4 5 4 0 4 5 4 0

Right now I type the points manually, but I want the code to read the text file itself so I don't type 900+ values manually.

Here is the format of the text file (file.txt):

A X 79 54 53 B Y 81 54 50 C Y 83 51 52

X and Y is extra information, not important for calculating distance. And here's the code I have (Thank you to user NetWallah for help with this):

my @points = ( {A=>A, X=>79.620, Y=>54.720, Z=>53.034}, {A=>B, X=>81.822, Y=>54.071, Z=>50.027}, {A=>C, X=>83.871, Y=>51.966, Z=>52.424}); my @results; for my $x(0..$#points){ my $p1 = $points[$x]; for my $y (0..$#points){ my $p2 = $points[$y]; my $dist = sqrt(abs(( $p1->{X} - $p2->{X} ) **2 + ( $p1->{Y} - $p2->{Y} ) **2 + ( $p1->{Z} - $p2->{Z} ) **2)) ; $results[$x][$y] = sprintf("%.0f", $dist); } } #------------------- for my $r (@results){ printf "%.0f\t",$_ for @$r; print "\n"; }

I added the "A=>" part because I wanted the matrix to show that parameter when it listed distance, but I have been unable to work that in properly.

This is the distance equation I am using:

$d = sqrt(($rx1 - $rx2)**2 + ($ry1 - $ry2)**2 + ($rz1 - $rz2)**2);

I would appreciate any advice on how to get the code to 1) read the values so I don't have to type them manually, and 2) print the matrix like so:

A B C A 0 4 5 B 4 0 4 C 5 4 0

Replies are listed 'Best First'.
Re: reading a text file and accepting input
by GrandFather (Saint) on Apr 28, 2012 at 00:11 UTC

    I see you have removed strictures (use strict; use warnings;) from the code NetWallah supplied, probably because they were telling you things you didn't understand and seem not to be bothered finding out about. So, rather than just giving you a solution to your problem, I want to see you do at least enough work to fix those errors. Show me the fixed code and I'll show you how to solve the rest of the problem.

    True laziness is hard work

      I mentioned in the OP that I was still working on the code in order to get the output as I would like it to present. But here's the code in a proper format:

      use strict; use warnings; my @points = ( {X=>79.620, Y=>54.720, Z=>53.034}, {X=>81.822, Y=>54.071, Z=>50.027}, {X=>83.871, Y=>51.966, Z=>52.424}, {X=>80.927, Y=>49.712, Z=>53.305}, {X=>80.565, Y=>46.114, Z=>52.104}, {X=>77.173, Y=>45.173, Z=>50.689}, {X=>75.652, Y=>41.722, Z=>51.009}); my @results; for my $x(0..$#points){ my $p1 = $points[$x]; for my $y (0..$#points){ my $p2 = $points[$y]; my $dist = sqrt(abs(( $p1->{X} - $p2->{X} ) **2 + ( $p1->{Y} - $p2->{Y} ) **2 + ( $p1->{Z} - $p2->{Z} ) **2)) ; $results[$x][$y] = sprintf("%.0f", $dist); } } #------------------- for my $r (@results){ printf "%.0f\t",$_ for @$r; print "\n"; }
      /code
Re: reading a text file and accepting input
by toolic (Bishop) on Apr 27, 2012 at 23:53 UTC
Re: reading a text file and accepting input
by Kenosis (Priest) on Apr 28, 2012 at 01:31 UTC

    This may, at least, get you started:

    use strict; use warnings; my $valuesFile = 'file.txt'; my (@points, $count); my @keys = qw(A X Y Z); open FH, $valuesFile or die "Can't open $valuesFile: $!"; while(<FH>){ chomp; my %pointHash; $pointHash{$keys[$count++]} = $_ for (split)[0, 2..4]; push @points, \%pointHash; $count=0; } close FH or die "Can't close $valuesFile: $!"; my @results; for my $x(0..$#points){ my $p1 = $points[$x]; for my $y (0..$#points){ my $p2 = $points[$y]; my $dist = sqrt(abs(( $p1->{X} - $p2->{X} ) **2 + ( $p1->{Y} - $p2->{Y} ) **2 + ( $p1->{Z} - $p2->{Z} ) **2)) ; $results[$x][$y] = sprintf("%.0f", $dist); } } #------------------- print "\t"; print "$points[$_]->{A}\t" for(0..$#points); print "\n"; $count = 0; for my $r (@results){ print "$points[$count++]->{A}\t"; printf "%.0f\t",$_ for @$r; print "\n"; }

    Output:

    A B C A 0 4 5 B 4 0 4 C 5 4 0

    Hope this helps!

      Your code calculates distance correctly, thank you. However, I think since I have a 360x360 matrix my output comes out weird when I transfer the data to a text file. Here's what it looks like:

      A B C A 0 4 5 4 3 4 3 B 4 0 4 6 4 4 3 C 5 4 0

      Any suggestions on how to correct this?

        The code to calculate distance isn't mine; it was in your original code.

        There may be some line wrapping going on in what you're showing. I created a 360x360 matrix and the results displayed fine.