Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
You could also use something along the lines of a dispatch table.
my %match = ( 'pattern_one' => 1, 'pattern_two' => 1, ); while (<F>) { next unless ( m/(match_pattern)/ ); print "PAGE ->\t$name\ndata ->"; print $match{$1} ? "\t\t$1\nMatched ->\t$hit\n" : " TEXT INFO HERE\n"; push(@files, $name); $ct++; } close(F);

In this case the %match is a little useless, as your blocks aren't really doing anything all that different aside from what to print. You are also using variables in the block that aren't being created in the block so its difficult to grasp exactly what you are trying to shorten. In a more complex case, the value of $match{pattern} could be a code ref, or any other data structure which could shorten the main loop a touch.. Something like

my %match = ( 'pattern_one' => [\&sub_one, $results_one], 'pattern_two' => [\&sub_two, $results_two], ); while (<F>) { chomp; next unless ( m/(match_pattern)/ ); unless ( $match{$1} ) { #something basic goes here next; } # get the right function from our dispatch table ($func, $results) = [ $match{$1} ]; # now call it, pass it the text to process, and store the # results. $results = &$func($1); } # END while <F> close(F);

In my own code, I tend to either make $results either an array or hash reference, depending on the situation, and the call may be altered to be say push(@$results, &$func($1)); or some such.

HTH, regards

use perl;


In reply to Re: Shorten script by l2kashe
in thread Shorten script by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found