August 1999 | Return to the Perl Hardware Store | Slide #31 |
This is a typical search-and-locate example (that is, it is a toy example)
Search a text file looking for matching lines
Obvious way:
sub locate { my ($key, $file) = @_; local *FH; open FH, $file or return; my @result; while (<FH>) { push @result, $_ if index($_, $key) >= 0; } @result; }
Maybe we spent a lot of time locating all the matching lines
Maybe we only needed a couple
Maybe the file was very big
Maybe several different parts of the program need to look for different things
Next | Copyright © 1999 M-J. Dominus |