in reply to Re^2: Split a binary file in pieces
in thread Split a binary file in pieces

Write out how you would do it in words, then translate words into code

Replies are listed 'Best First'.
Re^4: Split a binary file in pieces
by ZJ.Mike.2009 (Scribe) on Mar 10, 2010 at 05:54 UTC

    You're right :) I'm thinking sometimes because the Perl community is too kind and helpful, I'm just tempted to become very very lazy :)

    Anyway I tried translating words into code and came up with the following ugly code, which seems to be working. But I know it's ugly :)

    use strict; use warnings; $/ = \20_000_000; #I've just split a 20-meg file open my $f1, "<", '1' or die $!; # piece 1 is named 1 binmode $f1; open my $f2, "<", '2' or die $!; # piece 2 is named 2 binmode $f2; while(my $x = <$f1>){ while(my $y = <$f2>){ open my $out, ">", "outfile" or die $!; binmode $out; print $out $x.$y; } }

    UPDATE: tried optimizing a few lines. It looks a little cleaner

    use strict; use warnings; $/ = \20_000_000; #I've just split a 20-meg file into 2 pieces open my $f1, "<", '1' or die $!; piece 1 is named 1 binmode $f1; open my $f2, "<", '2' or die $!; # piece 2 is named 2 binmode $f2; while(defined(my $x = <$f1>) && (my $y = <$f2>)){ open my $out, ">", "outfile" or die $!; binmode $out; print $out $x.$y; }