Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

So naturally the file is huge? Any suggestions?

First whitespace is your friend aka learn how to format and indent your code.

Second every time you find a file that ends in .9 you overwrite the file 'adjusted.learn' (if there is more than one in that dir) with the '#do something' processed content of the .9 file so there is absolutely no possibility whatsoever of the 'adjusted.learn' file ever getting bigger than the size of the largest 'do something' munged .9 file in that dir unless you are doing something to make that so.

Here is your code. I have formatted it and changed a few minor things. Have a look at the changes..... besides the whitespace there are useful error messages like can't read (this file name in this dir) because of this reason rather than what your code would generate - ie cannot read file: access denied. That sort of error is useless. We know there is a problem. But what the hell is it! If you write code like you have presented you may as well write open F, $file or die "Error 123\n" for all the informative value you are adding. The only things you will genreally ever see in $! are 'access denied' or 'file does not exist', but you really want to know WHICH FILE WHERE?

Also if you get data in $_ in a sub it is generally not a good idea to mess with $_ withing that sub. See the while loop.

Leaving all that asside if you set $/ the input record separator to 'blah blah blah' and the string 'blah blah blah' does not appear in the input file you will read the entire file into memory as a single record. Thus $outlines[0] will contain the entire file content. It that your real problem?

find (\&process, $folder); sub process { if ($_ =~ /\.9$/) { print "\nProcessing file $File::Find::dir/$_\n"; open FILE, $_ or die "Cannot read $File::Find::name: $!"; local $/= "# input for"; # localise @outlines so we don't accumulate my @outlines = (); while ( my $line = <FILE> ) { #do something push @outLines, $line; } close FILE; my $out = 'adjusted.learn'; open OUTFILE, ">$out" or die "Cannot write $out: $!"; print OUTFILE @outLines; close OUTFILE; } } exit;

cheers

tachyon


In reply to Re: Working with Subfolder by tachyon
in thread Working with Subfolder 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? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2023-02-03 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (24 votes). Check out past polls.

    Notices?