in reply to Problem with multiple loops
Despite advice in an earlier thread, you have chosen to read the entire file into an array in memory, rather than dealing with just one line at a time. (This is after you have already read the file once just to count the lines -- but you would know the line count by checking the size of the array.)
On top of that, you make a complete second copy of the file contents in memory, making a second array in reverse order. Hint: data storage in Perl always consumes more memory than you might expect. When memory required by data exceeds physical memory on the machine, your process begins to use swap space (memory-resident data needs to be swapped out to disk when not in use, and swapped back in when needed). That slows you down tremendously.
Then, for some reason, you have chosen to open an output file, write just one line to it, and then close it, and you have to do this until you've processed the number of lines intended for the given output file, using "pop @rev_all_lines". (Why didn't you just use "shift" on the original array?)
The OS/system-lib overhead of opening and closing files can become profoundly significant when this is done many thousands of times in a single process. The time spent per unit of data being processed becomes remarkably long when only a small amount of data is being moved for each open/close cycle. (We all prefer to open a file once, write everything to it till it's done, then close it.)
Knowing from the earlier threads that you are just trying to split a big file into a small number of smaller files, I'm trying to figure out what is actually going on here with your arithmetic and loops. It's pretty obscure...
my $fn_count = $ARGV[0] - ($ARGV[0] - 1); # doesn't that just always set $fn_count = 1 ? # (is it a goal of this class project to write obfuscated code?)
But probably the worst thing is that you have two while loops, one nested within the other; the outer loop iterates over the number of output files requested on the command line. If that number is "4", then the input file must be read, beginning to end, 8 times, two copies of its contents must be created (and then destroyed/garbage-collected) 4 times, and the output file must be opened and closed as many times as there are lines to be written to that file. You absolutely do not need more than one while loop.
If you were running on a multi-user system, I would expect other users may have been resenting all this, because I'm sure they would have been noticing the consequences...
Altogether, the logic of the OP code falls into the category of "delete it and start over". I hope you'll be able to do that before the due date for the assignment.
Perl happens to be a language where decent, coherent pseudo-code can translate pretty directly into executable, efficient code. But you have to start with decent, coherent pseudo-code. The way I usually push this issue is to say: start by documenting clearly and concisely what the code will do and how it will do it (in other words, figure out and describe what the solution will look like first), then write the code according to the documentation.
Oh... and I don't know if anyone answered this question yet:
what to do if there is a remainder? Is there a way to scoop up the remainder as and save it as a $scalar?
That's what the "modulo" operator ("%") is for:
$remainder = ( 12 % 5 ); # i.e. 2 $remainder = ( 14 % 7 ); # i.e. 0 # etc...
|
|---|