Leser: 1
|< 1 2 3 >| | 21 Einträge, 3 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use strict;
use warnings;
my @ids = (1, 3, 4, 12, 41, 43, 44);
my $id = 4;
my $prev_id = 'undef';
my $next_id = 'undef';
for (my $i = 0; $i < scalar(@ids); ++$i)
{
if ($ids[$i] == $id)
{
$prev_id = $ids[$i-1] if ($i > 0);
$next_id = $ids[$i+1] if ($i+1 < scalar(@ids));
last;
}
}
print qq(ID: $id => NEXT: $next_id => PREV: $prev_id\n);
1 2 3 4 5 6 7 8 9
use strict; use warnings; my @ids = (1, 3, 4, 12, 41, 43, 44); my $id = 4; my $prev_id = defined($ids[$id-1]) ? $ids[$id-1] : 'undef'; my $next_id = defined($ids[$id+1]) ? $ids[$id+1] : 'undef'; print qq(ID: $id => NEXT: $next_id => PREV: $prev_id\n);
1
2
3
4
5
6
7
8
9
10
11
12
my @ids = (1, 3, 4, 12, 41, 43, 44);
my $id = 4;
sub addID {
for ( $i=0; $i<scalar(@_); $i++ ) {
$idMap{$_[$i]} = $i;
}
}
addID(@ids);
printf("ID: $id, NEXT: %d, PREV: %d\n", $ids[$idMap{$id}+1], $ids[$idMap{$id}-1]);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/usr/bin/perl use strict; use warnings; my @ids = qw/1 3 4 12 41 43 44 55 67 78 103 156 167 176 198 244 300 345 355/; my $id = 4; my $prev_id = 'undef'; my $next_id = 'undef'; if($id>=$ids[0] && $id<=$ids[-1]) { my $abstand=($ids[-1]-$ids[0])/scalar(@ids); my $pos=int($id/$abstand); if($ids[$pos]<$id) { $pos+=1 while($id>$ids[$pos]); } elsif($ids[$pos]>$id) { $pos-=1 while($id<$ids[$pos]); } if($id==$ids[$pos]) { $prev_id =$ids[$pos-1] if($pos-1>=0); $next_id =$ids[$pos+1]if($pos+1<=$#ids); } } print "VOR ID: $prev_id\n"; print " ID: $id\n"; print "NACH ID: $next_id\n";
|< 1 2 3 >| | 21 Einträge, 3 Seiten |