#!/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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |