#!/usr/bin/perl use strict; use warnings; #Open data file and put it into variable open FILE, "<data.txt"; my $string = do {local $/; <FILE> }; #Set offset if there is a header record and add it to initial position my $offset = 0; my $currentpos = 0 + $offset; #Set lastpos = length of data my $lastpos = length($string); #Read until end of data while ($currentpos < $lastpos) { #Grab record length within data my $recordlength = (substr $string, $currentpos, 4); #Grab record type which is 5th char my $recordtype = substr $string, $currentpos+4, 1; #Using record length grab the record and put it into variable my $fragment = substr $string, $currentpos, $recordlength; #Calculate starting position for next record .. not used at present my $nextstartpos = $currentpos + $recordlength + 1; #Use if clause to identify only $ record types and print the record da +ta if ($recordtype eq '$') { print " record type: $recordtype\n"; print " data: $fragment\n"; #Inrement starting position for the next loop $currentpos = $currentpos + $recordlength; } }

Any ideas?

I've commented to try and explain what it does, it's working through a file identifying records of different types within it and printing certain record types to screen however it just hangs after printing the 1st record for me

Thanks!


In reply to 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.