my $pos = 3;
my $i;
foreach $i (1..5) {
print( ("*"," ","|")[ $i <=> $pos ], " $i\n");
};
####
my @list = ("foo", "bar", "baz", "toto", "tata");
$pos = "baz";
foreach $i (@list) {
print( ("*"," ","|")[ $i cmp $pos ], " $i\n") if $print;
};
####
my $index = 0;
foreach (@list) {
last if $pos eq $_;
$index++;
};
foreach $i (0..$#list) {
print( ("*"," ","|")[ $i <=> $index ], " $list[$i]\n");
};
####
my $seen = 0;
$index = grep { $seen |= ($_ eq $pos); ! $seen; } @list;
foreach $i (@list) {
print( ("*"," ","|")[ 0 <=> $index-- ], " $i\n");
};
####
# And now the hardcore, nonobvious solution, leaving the grep
# loop prematurely and discarding the result :
$index = 0;
grep { goto DONE if ($_ eq $pos); $index++; 0 } @list;
DONE:
foreach $i (@list) {
print( ("*"," ","|")[ 0 <=> $index-- ], " $i\n");
};
####
# An inline function, that does not assume that $pos
# is in @list :
$seen = - $#list -1;
foreach $i (@list) {
if ($i eq $pos) {$seen = 0} else { $seen++ };
print( ("*"," ","|")[ $seen <=> 0 ], " $i\n");
};