in reply to reading a text file and accepting input

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!

Replies are listed 'Best First'.
Re^2: reading a text file and accepting input
by doozy (Initiate) on Apr 28, 2012 at 02:01 UTC

    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.