Hi guys, I'm a Perl newbie (but I love it already!) and I'm struggling with one of my first programs... I have to read a bin file which contains recors like this:
1 5.7 -12.3 2.1 0 1 225.3 0 0 1 3.24 2.98 -10.02 1 2 334.7 ...
which has to be interpretated as follows:
1) read the first value (1 byte, integer)
2) if it's larger than 0, read the following 6 values which are 3 doubles (8bytes), 2 long integer (4bytes) and another double (8bytes again). Store the values and restart from 1)
3) if it's 0, then go back to 1)

In matlab this is rather easy, but extremely slow for the dimesnions of my files (it takes more than one hour to read one of them). I implemented it as follows:
[photon1, c] = fread(fidin, [1], 'int8'); if (photon1>0) [readout(1,1:3), c] = fread(fidin, [3], 'double'); [readout(1,4:5), c] = fread(fidin, [2], 'int32'); [readout(1,6), c] = fread(fidin, [1], 'double'); end

I'm using read() and unpack() to read the file but i'm getting the wrong numbers. I'm surely making mistakes in the interpeetation of the type of data. Here is my perl code:
read(BIN_DATA, $bin_data, $short_int_size); # Convert from binary to a numeric value $photon1 = unpack('C', $bin_data); if ($photon1>0) { read(BIN_DATA, $bin_data, $double_size); $x1 = unpack('f', $bin_data); read(BIN_DATA, $bin_data, $double_size); $y1 = unpack('f', $bin_data); read(BIN_DATA, $bin_data, $double_size); $z1 = unpack('f', $bin_data); read(BIN_DATA, $bin_data, $int_size); $obj_scatt1 = unpack('L', $bin_data); read(BIN_DATA, $bin_data, $int_size); $coll_scatt1 = unpack('L', $bin_data); read(BIN_DATA, $bin_data, $double_size); $en1 = unpack('f', $bin_data); }

I'm sure I didn't hike all the way up to the mountain top in vain, and that your wisdom will enlight me to solve this problem. I thank you in advance for that.

Andri

In reply to read + unpack by andri85

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.