I would use unpack or regexen or substr to extract the data ... the key is to move the values into variables on a reliable basis.

#!/usr/bin/perl use strict; use warnings; #Open data file and put it into variable open FILE, "<data.txt"; my $string = do {local $/; <FILE> }; my ($idx, $max) = (0, length $string ); while ( $idx < $max - 4 ) { my $fragment= substr( $string, $idx, 4 ); $idx+= length $fragment; my $recordtype = substr( $string, $idx, 1 ); $idx++; if ($recordtype eq '$') { print " record type: $recordtype\n"; print " data: $fragment\n"; } else { print "Non-dollar fragment: '$fragment', '$recordtype'\n"; } } print "Unused portion of data file: ", substr( $string, $idx );

You will deduce I had a line break in my data.

record type: $ data: 1234 Non-dollar fragment: 'qwer', 't' Non-dollar fragment: 'asdf', 'g' Non-dollar fragment: 'zxcv', 'b' record type: $ data: !@#$ record type: $ data: ?:"

Notice that if i come within 4 characters of the end, there are characters which can't be used duetot he lack of a recordtype.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.


In reply to Re: Can someone tell me why this hangs?? by TomDLux
in thread Can someone tell me why this hangs?? by monty77

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.