Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Split up a string using a rege ...Absolute value function (Berikutnya)
Regular expressions and pattern matching

Optimize input data for repeated searches

Daftar Isi

  • study SCALAR

  • study

    Takes extra time to study SCALAR ($_ if unspecified) in anticipation ofdoing many pattern matches on the string before it is next modified.This may or may not save time, depending on the nature and number ofpatterns you are searching and the distribution of characterfrequencies in the string to be searched; you probably want to comparerun times with and without it to see which is faster. Those loopsthat scan for many short constant strings (including the constantparts of more complex patterns) will benefit most.(The way study works is this: a linked list of everycharacter in the string to be searched is made, so we know, forexample, where all the 'k' characters are. From each search string,the rarest character is selected, based on some static frequency tablesconstructed from some C programs and English text. Only those placesthat contain this "rarest" character are examined.)

    For example, here is a loop that inserts index producing entriesbefore any line containing a certain pattern:

    1. while (<>) {
    2. study;
    3. print ".IX foo\n" if /\bfoo\b/;
    4. print ".IX bar\n" if /\bbar\b/;
    5. print ".IX blurfl\n" if /\bblurfl\b/;
    6. # ...
    7. print;
    8. }

    In searching for /\bfoo\b/, only locations in $_ that contain fwill be looked at, because f is rarer than o. In general, this isa big win except in pathological cases. The only question is whetherit saves you more time than it took to build the linked list in thefirst place.

    Note that if you have to look for strings that you don't know tillruntime, you can build an entire loop as a string and eval that toavoid recompiling all your patterns all the time. Together withundefining $/ to input entire files as one record, this can be quitefast, often faster than specialized programs like fgrep(1). The followingscans a list of files (@files) for a list of words (@words), and printsout the names of those files that contain a match:

    1. $search = 'while (<>) { study;';
    2. foreach $word (@words) {
    3. $search .= "++\$seen{\$ARGV} if /\b$word\b/;\n";
    4. }
    5. $search .= "}";
    6. @ARGV = @files;
    7. undef $/;
    8. eval $search; # this screams
    9. $/ = "\n"; # put back to normal input delimiter
    10. foreach $file (sort keys(%seen)) {
    11. print $file, "\n";
    12. }
 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Split up a string using a rege ...Absolute value function (Berikutnya)