You didn't post your updated code, or say how it "doesn't work for" you.

Did you read the documentation for read? Did you understand that you will have to tell it how many bytes to read in each call to read()? As a result, instead of slurping the whole file into an array using @r = <$fh>, you will either do one bulk read (which assumes you know the file length to begin with; that's possible, but it's left as an exercise for the reader, if you really want to go down that route, to research how to find the file size), or you will have to do an individual read for each 36-byte record (which would be my recommendation), or do reads for each sub-element of the record (which would be a more complicated loop structure).

If you go down the road of a read() for each group of 36-bytes, I'll give you some more hints: in looking at the return values for read() in the documentation, you should be able to come up with a loop construct that will stop once you've reached the end of the file, and processes each individual record very similarly to how you processed each group of 36 bytes from the joined $data scalar.

For example, I took your original code, converted over to the array/join you tried; then I output some equals, and reset to the beginning of the file handle and re-read it using a read() loop, parsing it very similarly (but appropriate for a $record that's exactly 36 bytes every time), so not using offsets in the substr. Given the input file

This is the Name____________00099999This is another name ______11199999This is the third name......22299999
...(with windows CRLF line ending taking up two bytes between "name" and "______" to ensure 36-byte record lengths)

I got the output

This is the Name____________ - 49:49:49 This is another name ______ - 50:50:50 This is the third name...... - 51:51:51 ============================== This is the Name____________ - 49:49:49 This is another name ______ - 50:50:50 This is the third name...... - 51:51:51

If you still cannot get the loop to work with read() after reading these hints and the documentation, feel free to post your updated code, and we can point out where you've gone wrong.


In reply to Re^5: Perl binary file reading by pryrt
in thread Perl binary file reading by kepler

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.