in reply to File Extraction - Cont...

If you are using MS-Windows, you probably need to do "binmode" on both the input and output files after you open them, to turn off "crlf" mode. If you don't do that, the binary data will get corrupted.

Apart from that, you really should set the input recorder separator variable to "AZII*" (as recommended in replies to your previous thread), because using line-oriented i/o on non-text data is just strange. Try something like this:

use strict; open( FILE, '1.dat') or die "1.dat: $!"; binmode FILE; $/ = "AZII*"; my $num = 0; while (<FILE>) { chomp; next unless length(); # skip the initial (empty) input record $num++; open (DES, ">out/$num.txt") or die ( "out/$num.tiff" ); binmode DES; print DES "AZII*".$_; close DES; last if ( $num == 5 ); }
(Updated to add the "next unless length()" condition -- if the file begins with "AZII*", the first input record will be empty after the "chomp".)

You may need to play with that, e.g. if "AZII*" is supposed to be followed by a line-feed (or carriage-return + line-feed).

I don't understand why the output file names in the OP were set to "$num.txt" -- these are not text files. You say they are image files (tiff), so why not use the appropriate extension for the output files? Am I missing something in your description of the problem?

Replies are listed 'Best First'.
Re^2: File Extraction - Cont...
by karavay (Beadle) on Sep 26, 2007 at 03:09 UTC
    I’ve used txt in this example as it is easier to debug check the output - i've actually use binmode in my code (probably deleted it while forming my request with the rest of the comments). Thanks for your reply I'll play around with it - the problem is that I'am a newbie to perl and still have a pretty poor vocabulary (really hope to improve it in the future and the only way to archive this is to practice and ask questions :) Thanks again,
Re^2: File Extraction - Cont...
by karavay (Beadle) on Sep 26, 2007 at 03:19 UTC
    your code has solved the problem - really appreciate your help