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

Re: Re: Re: Re: Shorten script

by snax (Hermit)
on Jul 22, 2003 at 20:48 UTC ( [id://276916]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Shorten script
in thread Shorten script

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.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Shorten script
by sauoq (Abbot) on Jul 22, 2003 at 21:02 UTC
    is the or a short circuit operator?

    Yes. That code should work fine.

    Now, if you take the final step and incorporate a ternary operator in the print statement, you'll have reproduced BrowserUk's version. :-)

    -sauoq
    "My two cents aren't worth a dime.";
    
      True, but I do so hate the ternary operator :)

      Well, that's not precisely true. With "complex" tests and returns I find it obfuscates the logic a bit (to my eye) -- truly a personal aesthetics issue.

      What I do like about BrowserUk's version is the use of $_; I should have noticed that right away. I'm slipping as work and hobby has kept me away from perl for too long. It's good to be back in the saddle again....

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://276916]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-20 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found