in reply to split a file to several smaller file

my $count = 0; my $fh_out; while (<$fh_in>) { if (!defined($fh_out)) { my $fname = sprintf('%s-%d.%s', $base, $count++, $ext); open($fh_out, '>', $fname) or die("Unable to create \"$fname\": $!\n"); } print $fh_out $_; if ($. % $num_lines == 0) { undef($fh_out); } }

Update: Fixed typo mentioned in reply.

Replies are listed 'Best First'.
Re^2: split a file to several smaller file
by cdarke (Prior) on Dec 11, 2007 at 13:14 UTC
    Slight typo, should be $! instead of $1 (<shift> key):
    or die("Unable to create \"$fname\": $!\n");
Re^2: split a file to several smaller file
by sardines (Novice) on Dec 24, 2007 at 02:27 UTC
    Hi, I have error when i run this.. could you give me more information as in what is $base and $ext? sorry.. i'm new to perl.. thanks!

      $base is the base name of all of the files, such as the log part of log001.txt, log002.txt, and log003.txt. $ext is the file extension, or txt in the example here.

        thanks.. and what is $fh_out? $fh_in is the text file it is reading right? Now there is no errors but I am not spawning any files.. there is no output..
Re^2: split a file to several smaller file
by sardines (Novice) on Dec 28, 2007 at 08:32 UTC
    thanks.. and what is $fh_out? $fh_in is the text file it is reading right? Now there is no errors but I am not spawning any files.. there is no output..

      and what is $fh_out? $fh_in is the text file it is reading right?

      Yes, except that these are "file handles" rather than the actual file in any sense, (hence the naming convention "fh").

      Does that make sense? If you do a close $fh_out; the file doesn't go away, just perl's ability to write to the file via that particular handle.

        Thanks for the explaination of file handler.. it make sense.. i did specified a fh_in. but i'm still not sure what does fh_out do? we have $base and $ext to work out the file name of the output so what does fh_out do?
      Thanks for the explaination of file handler.. it make sense.. i did specified a fh_in. but i'm still not sure what does fh_out do? we have $base and $ext to work out the file name of the output so what does fh_out do?
        Just as '$fh_in' is the file handle for input, '$fh_out' is the file handle for output. The print function checks its first argument (the first thing that comes after the word "print"), and if this arg turns out to be a file handle, then the rest of the args are printed to that file; if the first arg after "print" is not a file handle, then print assumes 'STDOUT' (the default output file handle) and prints all its args to that "file" (which is usually your terminal window).

      Did you actually open a file and assign the handle to $fh_in?

      open(my $fh_in, '<', $qualified_name) or die("Unable to open file \"$qualified_name\" to split: $!\n");
Re^2: split a file to several smaller file
by sardines (Novice) on Dec 31, 2007 at 02:55 UTC
    hey.. it is working now.. thank guys! great help!