4 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
my @ar=(4,1,0,9,6,7,4,0,2);
my $count=0;
my $spanne=0;
my $take=0;
my $vorgabe=3;
my @ar2;
foreach (@ar)
{
if ($_ <= $vorgabe-$spanne)
{
$take = $count;
$spanne = $_;
push(@ar2,$_);
}
$count++;
}
print $_."\n" foreach @ar2;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#! /usr/bin/perl
use strict;
use warnings;
my $vorgegeben = $ARGV[0];
my @ar=(4,1,0,9,6,7,4,0,2);
my %hash;
foreach my $i(@ar){
push(@{$hash{abs(($vorgegeben - $i))}},$i);
}
my $key = (sort{$a <=> $b}keys(%hash))[0];
print $_,"\n" for(@{$hash{$key}});
1
2
3
my @array = (4, 1, 0, 9, 6, 7, 4, 0, 2);
my $v = 5;
print grep {abs($_-$v) == abs((sort {abs($a-$v) <=> abs($b-$v)} @array)[0]-$v)} @array;
4 Einträge, 1 Seite |