in reply to Splitting file into separate files based on record lengths and identifiers
You could do this quite compactly using embedded Perl code in a regular expression, but given that you are a "complete novice", a simpler algorithm of wrapping a substitution in a while loop would likely be sufficient. I assume you know how to open files and print output, so I will give you a simple demonstration of how you might implement your algorithm using regular expressions.
#!/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"; }
outputs:
Type: <$> Record: <ADAM> Type: <*> Record: <33> Type: <%> Record: <1977> Type: <$> Record: <BOB > Type: <*> Record: <43> Type: <%> Record: <1967>
If you have questions about how this works, I'd be happy to expound, though you should be able to find any answer in perlre and/or perlretut.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Splitting file into separate files based on record lengths and identifiers
by monty77 (Initiate) on Aug 25, 2010 at 22:14 UTC | |
by kennethk (Abbot) on Aug 25, 2010 at 22:31 UTC |