in reply to Re: IO::File Question
in thread IO::File Question

Thank you for your response.

This little problem is part of a larger class assignment. Having already looked at the UNIX man page for ‘split’ can definitely I see your point. However I think the idea is more about me to learning about IO::File than actually finding the most efficient way of doing this particular problem and so don’t want to risk being too clever for my own good here.

I realize that this maybe inefficient, but I think your idea of using a line counter is very good. Would you need one or two counters? I know you would need to have one to count the number of lines written to each file (800 in this example) but would you not also want a counter to keep track of which new output file you are writing to and then pasting that on the end of the new file (4 would be made in this example) name?

-mox

Replies are listed 'Best First'.
Re^3: IO::File Question
by ikegami (Patriarch) on Oct 23, 2006 at 06:20 UTC
    You don't actually need a counter, since Perl already provides one: $.
    my $max_lines = 800; if (($. - 1) % $max_lines == 0) { my $file_num = int(($. - 1) / $max_lines) + 1; my $file_name = "file${file_num}.txt"; open($fh_out, '>', $file_name) or die("Unable to create file \"$file_name\": $!\n"); }
Re^3: IO::File Question
by graff (Chancellor) on Oct 23, 2006 at 05:15 UTC
    I know you would need to have one to count the number of lines written to each file (800 in this example) but would you not also want a counter to keep track of which new output file you are writing to and then pasting that on the end of the new file (4 would be made in this example) name?

    Yes, you'd need to keep a counter that you increment each time you open a new output file, as well as a counter that you increment each time you write to the current output file (and that gets reset to zero each time you open a new file).

    And you can still use IO::File to manage the opening and closing of files, and use lexically scoped scalar variables as "file handle objects".