http://qs321.pair.com?node_id=964482


in reply to splitting file with mutiple output filenames

Ok, that was a serious and embarrasing 'duh' moment. I forget to put the input filehandle name after foreach. So it should be: foreach (<THAI>) { Sorry for the waste of time.

Replies are listed 'Best First'.
Re^2: splitting file with mutiple output filenames
by GrandFather (Saint) on Apr 11, 2012 at 09:31 UTC

    Ok, you solved problem, but let's make the post worth while by passing on the usual round of "wisdom". Consider:

    #!/usr/bin/perl # chess.plx use warnings; use strict; use utf8; my $filename = "thaikjv-fixed.txt"; open my $fIn, '<', $filename or die "Can't open $filename: $!\n"; binmode ($fIn, ":utf8"); while (defined (my $line = <$fIn>)) { next if $line !~ /^@(...\d\d\d)/; my $outname = "$1.html"; open my $fOut, '>', $outname or die "Can't create $outname: $!\n"; binmode ($fOut, ":utf8"); print $fOut $line; print "hi"; close $fOut; }

    Note:

    • use three parameter version of open and lexical file handles
    • show the file name in errors
    • use a while loop instead of a for loop when reading files
    • use early exit to avoid extra levels of indentation
    • avoid using the default variable across multiple lines
    True laziness is hard work
Re^2: splitting file with mutiple output filenames
by Anonymous Monk on Apr 11, 2012 at 09:24 UTC
    I'd suggest to use while(<FH>) instead of foreach(<FH>). (while loop reads directly from file, for(each) loop slurps the entire file into memory first - really bad for huge files, but ok for small ones).

    Maybe something like this:
    use strict; use warnings; open my $thai_fh, '<:utf8', "thaikjv-fixed.txt" or die $!; while(<$thai_fh>){ if (/^@(...\d\d\d)/) { open my $out_fh, '>:utf8', "$1.html" or die $!; print {$out_fh} $_; } }
    /code