Help for this page

Select Code to Download


  1. or download this
    my $pos = 3;
    my $i;
    ...
    foreach $i (1..5) {
      print( ("*"," ","|")[ $i <=> $pos ], " $i\n");
    };
    
  2. or download this
    my @list = ("foo", "bar", "baz", "toto", "tata");
    $pos = "baz";
    foreach $i (@list) {
      print( ("*"," ","|")[ $i cmp $pos ], " $i\n") if $print;
    };
    
  3. or download this
    my $index = 0;
    foreach (@list) {
    ...
    foreach $i (0..$#list) {
      print( ("*"," ","|")[ $i <=> $index ], " $list[$i]\n");
    };
    
  4. or download this
    my $seen = 0;
    $index = grep { $seen |= ($_ eq $pos); ! $seen;  } @list;
    ...
    foreach $i (@list) {
      print( ("*"," ","|")[ 0 <=> $index-- ], " $i\n");
    };
    
  5. 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");
    };
    
  6. 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");
    };