re: Challenge: Get Closest Number in an Array VIEW POST

VIEW FULL DISCUSSION
 

Perl solution:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

use List::AllUtils qw{ min_by };

my @nums = (100, 200, 400, 800, 1600, 3200, 6400, 128000);
my $given_num = 900;

say $nums[ min_by { abs($nums[$_] - $given_num) } 0 .. $#nums ];
code of conduct - report abuse