[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] newbie question: binary file manipulation



On Thu, 12 Aug 1999 00:09:11 -0300, Sébastien Maury wrote:

>	I am learning perl to do file manipulation. I managed to do 
>what I want with text file but I have to convert big (about 100Mo) 
>text data file (list of points coord.) in binary file. Is it possible 
>? For example, I would like to convert line input :
>
>x = 12.1 y = 13.4
>
>in the binary form
>XXXX YYYY (float on 4 bytes)
>
>Is there a special option in the open statement or something like that ?

You mean binmode()? No; although this is necessary for good results on a
PC (DOS, Windows), it's a noop on MacPerl. 

What you need to do, is use pack(), i.e. explicitely convert the
text/numbers into a binary structure of 8 bytes. You can use write(),
syswrite(), but also a plain and simple print() to store that data into
a file. See the pack() entry in perlfunc.pod.

	$numpat = '-?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?';
	open BINFILE,">binary.dat";
	binmode BINFILE; # good habit
	$_ = "x = 12.1 y = 13.4";
	if(/\bx\s*=\s*($numpat)\W+y\s*=\s*($numpat)/o) {
	    print STDERR "X=$1; Y=$2\n";
	    print BINFILE pack('ff',$1,$2);
	}

	Bart.

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org