Dear Monks,
This question is in a similar vein to that asked by Dwalin (#685248) and from how I gather seems a hot-topic for questions. I should also mention that I asked a very similar question in another forum but no response was given. Therefore, I hope it isn't too trivial a problem.
Anyway, I have written a script that outputs data to a binary format, via the rather simple statement.
open (OUTFILE, ">","$filename"); binmode OUTFILE, ":raw"; foreach $out (@data){ print OUTFILE pack('V', $out); } close OUTFILE;
Now this file is manipulated by an old program but leaves it in the same low-endian form. What I would like to do is read back the binary file into a perl script and then further manipulate it. From a suggestion found in (#685248), I wrote the following test script:
#!/usr/bin/perl -w $filename = "data.spc"; $recsize = 4; $x = 1; $i = 0; open (FILE, "<", "$filename") or die "$!"; while (read(FILE, $buf, $recsize) == $recsize) { @values = unpack("V", $buf); @datapoint = splice(@values, 0, $x); $i += 1; } printf ("%d\n",$i); #No. of loops performed unlink "temp.dat"; open (OUTFILE, ">", "data.dat"); foreach $datapoint (@datapoint){ printf OUTFILE ("%d\n",$datapoint); } close OUTFILE;
The thing that has absolutely confused me is that while the number of iterated loops displayed is correct, but the data.dat file contains only a single number one, rather than 16,383 data points that it 'suggestively' looped over. Also, changing the value of $x did not seem to affect this result.
So does the fault lie in the unpacking of it, the splicing of it, or a very trivial error that I have not spotted? Also, in the unpack statement, I assume that the format indicates what the data should be read as.
Comments and suggestions on this matter would be very much appreciated.
Regards
Steve
In reply to Simple query regarding unpack/splice by sf_ashley
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |