Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here are some thought about your code.

  • Check for the success or failure of open and close operations.

  • Use the three argument form of open and lexical filehandles.

  • Don't keep opening and closeing your output file every time you find a line you are interested in, do them once outside the while loop. Your code keeps opening the file for writing thereby clobbering what you have written in previous loops.

  • Add captures to your regex to preserve what comes before and after $element and avoid the performance implications of $'.

  • Reading into $_ can save some typing as certain operations default to using $_ if no argument is given.

Something along these lines (not tested).

use strict; use warnings; my $element = q{coucou}; my $inFile = q{EXEMPLE.txt}; my $outFile = qq{RESEARCHED_text_${element}.txt}; open my $inFH, q{<}, $inFile or die qq{open: $inFile: $!\n}; open my $outFH, q{>}, $ourFile or die qq{open: $outFile: $!\n}; while ( <$inFH> ) { next unless m{(.*?)$element(.*); print $outFH; my $beforeElement = $1; my $afterElement = $2; # Do something here with your captured text ... } close $inFH or die qq{close: $inFile: $!\n}; close $outFH or die qq{close: $outFile: $!\n};

I hope this is of use.

Cheers,

JohnGG


In reply to Re: get the rest of the text by johngg
in thread get the rest of the text by steph_bow

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 making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-16 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found