in reply to element of an array of arrays
UPDATE ... populate a look-back array with rows from the original data. ... The flow concept goes like this:
run through the original data via foreach. For every original data row :
while there are less than 15 rows in the look-back array, push each original data row onto the look-back array
after there are 15 rows in the look-back array, scan the look-back array for the specified value,
then shift the first row off of the look-back array and push the current original data row onto the look-back array
repeat until the end of the original data
Is this flow concept so far off ...?
Actually, that seems like a nice approach — assuming you understand what's really in the rows you're dealing with!
Of course, there are always Other Ways To Do It. If the data in a typical row was very large (megabytes) and you really needed to maximize speed and so to minimize data movement, an approach based solely on array references and manipulation of array indices might be productive.
>perl -wMstrict -le "my @ra = (0, 1, 42, 3, 4, 5, 6, 7, 8, 42, 10, 42, 12, 42); ;; for my $i (0 .. $#ra) { look_back_5_for_42(\@ra, $i); } ;; sub look_back_5_for_42 { my ($ar, $current_i) = @_; ;; my $back = $current_i - 5 + 1; $back = 0 if $back < 0; ;; printf qq{at index $current_i: }; for my $i ($back .. $current_i) { printf qq{42 at index $i, } if $ar->[$i] == 42; } print ''; } " at index 0: at index 1: at index 2: 42 at index 2, at index 3: 42 at index 2, at index 4: 42 at index 2, at index 5: 42 at index 2, at index 6: 42 at index 2, at index 7: at index 8: at index 9: 42 at index 9, at index 10: 42 at index 9, at index 11: 42 at index 9, 42 at index 11, at index 12: 42 at index 9, 42 at index 11, at index 13: 42 at index 9, 42 at index 11, 42 at index 13,
|
|---|