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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.