Since you've gotten lots of sample code to look at, let me focus just on the code you posted. I recall from your earlier post about this class assignment that you are dealing with a rather large file, and since your main probem is that your script is too slow, my comments are mostly about optimizing.

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...

In reply to Re: Problem with multiple loops by graff
in thread Problem with multiple loops by chinamox

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.