Thread suche in einem array (20 answers)
Opened by bo at 2006-12-21 12:12

bo
 2006-12-21 12:12
#72662 #72662
User since
2006-05-09
76 Artikel
BenutzerIn
[default_avatar]
hi community,

ich suche innerhalb eines arrays den vorgänger und den nachfolger eines elements
gibt es dafür eine elegantere lösung?

Code: (dl )
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);

View full thread suche in einem array