Answer to Question #15325 in Perl for Andrew

Question #15325
How to create a perl program that finds the distance between two points in an array?
1
Expert's answer
2012-11-08T09:14:04-0500
#!/usr/bin/perl
use strict; use warnings;
use List::Util qw( min );
use Statistics::Descriptive;

my $stat = Statistics::Descriptive::Full->new;
my @long = (100, 200, 210, 300, 350, 400, 401, 402, 403, 404, 405, 406);
my @short = (3, 6, 120, 190, 208, 210, 300, 350);

for my $x ( @short ) {
$stat->add_data(find_dist($x, @long));
}

my $freq = $stat->frequency_distribution_ref([0, 2, 10, 20, 94, 97]);
for my $bin ( sort { $a <=> $b } keys %$freq ) {
print "$bin: $freq->{$bin}
";
}

sub find_dist {
my ($x, $v) = @_;
return min map abs($x - $_), @$v;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS