Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: splitting file with mutiple output filenames

by preahkumpii (Novice)
on Apr 11, 2012 at 09:17 UTC ( [id://964482]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://964482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found