Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Working with Subfolder

by tachyon (Chancellor)
on May 28, 2004 at 11:53 UTC ( [id://357192]=note: print w/replies, xml ) Need Help??


in reply to Working with Subfolder

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

Replies are listed 'Best First'.
Re: Re: Working with Subfolder
by Anonymous Monk on May 28, 2004 at 12:13 UTC
    i have used your suggestions, however, i am still getting the same problem. Processed the first file fine, then the second processed file, lists both and so on!

      You need to empty @outlines as noted in the code above. Otherwise each call to process() will add more lines to @outlines.

      cheers

      tachyon

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2024-04-24 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found