#!/usr/bin/perl use strict; use warnings; my $input = '0004$ADAM0002*330004%19770004$BOB 0002*430004%1967'; while( length $input ) { unless ($input =~ s/^(\d+)(.)//) { die "Input misformatted: $input"; } my $len = $1; my $type = $2; unless ($input =~ s/^(.{$len})//) { die "Input misformatted: $len, $type, $input"; } my $record = $1; print "Type:\t<$type>\nRecord:\t<$record>\n\n"; } #### Type: <$> Record: Type: <*> Record: <33> Type: <%> Record: <1977> Type: <$> Record: Type: <*> Record: <43> Type: <%> Record: <1967>