Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Do you know where your variables are?
 
PerlMonks  

Re: Shorten script

by sauoq (Abbot)
on Jul 22, 2003 at 14:58 UTC ( [id://276877]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Shorten script

while($line = <F>) { for my $hit ($line =~ /matchPattern/gi) { $ct++; push @files, $name; print "PAGE ->\t$name\ndata ->"; if( $line =~ /(patternOne)/i ) { print "\t\t$1\nMATCHED - >\t$hit\n"; } elsif( $line =~ /(patternTwo)/i ) { print "\t\t$1\nMATCHED - >\t$hit\n"; } else { print " TEXT INFO HERE.\n"; } } }
-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Shorten script
by snax (Hermit) on Jul 22, 2003 at 16:15 UTC
    Since you're printing more or less the same thing, see if you can combine the patterns you're matching against thusly:
    while($line = <F>) { for my $hit ($line =~ /matchPattern/gi) { $ct++; push @files, $name; print "PAGE ->\t$name\ndata ->"; if( $line =~ /(patternOne|patternTwo)/i ) { print "\t\t$1\nMATCHED - >\t$hit\n"; } else { print " TEXT INFO HERE.\n"; } } }

      In general, it's more efficient to elevate the "ors" out of the pattern. In other words,

      $line =~ /pat1/ or $line =~ /pat2/
      is usually better than a single more complex regular expression.

      -sauoq
      "My two cents aren't worth a dime.";
      
        Well, yes, but he did say he was trying to shorten his script :)

        I am often a chooser of elegance (subjective, of course) over efficiency.

        So, to combine some efficiency with some elegance:

        while($line = <F>) { for my $hit ($line =~ /matchPattern/gi) { $ct++; push @files, $name; print "PAGE ->\t$name\ndata ->"; if( $line =~ /(patternOne)/i or $line =~ /(patternTwo)/i ) { print "\t\t$1\nMATCHED - >\t$hit\n"; } else { print " TEXT INFO HERE.\n"; } } }
        The $1 will be properly remembered -- although if a line matches both patterns is the or a short circuit operator? You might have to switch the order to keep the behavior identical.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://276877]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.