in reply to Padding search results with context
Not sure if it's better or just different, but you could do something like this:
my @nums = (1, 5, 6, 8, 15, 20); my $max_idx = 21; my $pad = 1; my @hilite = @nums; my $hilite = shift @hilite; my $i = -1; for my $n (@nums) { for my $c ($n-$pad .. $n+$pad) { if ($c > $i) { last if $c > $max_idx; $i = $c; if ($i == $hilite) { print "I=$i\n"; $hilite = shift @hilite; } else { print " i=$i\n"; } } } }
Output:
i=0 I=1 i=2 i=4 I=5 I=6 i=7 I=8 i=9 i=14 I=15 i=16 i=19 I=20 i=21
Update: changed context to $pad = 1 to keep the example output in line with the other replies (Apparently it was asking too much to figure out that I had originally set $pad = 2 ... — Or why the downvotes?)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Padding search results with context
by moritz (Cardinal) on Aug 14, 2007 at 06:55 UTC |