Advertisement
cd62131

process data(x y)

Jul 18th, 2014
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.62 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use feature 'say';
  5. sub dist {
  6.   my ($x1, $y1, $x2, $y2) = @_;
  7.   return sqrt(($x1 - $x2) * ($x1 - $x2) + ($y1 - $y2) * ($y1 - $y2));
  8. }
  9. my $data = [];
  10. my $result = [];
  11. while (<>) {
  12.   chomp;
  13.   my @d = split;
  14.   push @$data, \@d;
  15. }
  16. my $size = scalar @$data;
  17. for (my $i = 0; $i < $size; $i++) {
  18.   for (my $j = $i + 1; $j < $size; $j++) {
  19.     my $r = [$data->[$i][0], $data->[$i][1], $data->[$j][0], $data->[$j][1]];
  20.     push @$r, dist @$r;
  21.     push @$result, $r;
  22.   }
  23. }
  24. @$result = sort { $a->[4] <=> $b->[4] } @$result;
  25. foreach my $r (@$result) {
  26.   say join "\t", @$r;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement