in reply to Can someone tell me why this hangs??

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.

Replies are listed 'Best First'.
Re^2: Can someone tell me why this hangs??
by monty77 (Initiate) on Aug 29, 2010 at 04:08 UTC

    Thanks everybody .. moving the $currentpos update out of the if fixed it beautifully.

    To answer one question, the record length is always 4 chars in length and as for the other suggestions they're very much appreciated but I've only been writing perl for a couple of days so my scripts are going to be a little amateurish at this stage - does the job though :)

    Cheers!