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