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 | |
by Kenosis (Priest) on Apr 28, 2012 at 05:57 UTC |