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