I think Javafan has it in terms of brevity. But here is yet another way. I changed the data a bit in an attempt to cover more cases. Worthy of note here is that some solutions, mine included essentially just "throw away" the field length information.
#!/usr/bin/perl -w use strict; use Data::Dumper; my %xlatetype = ('$' => 'string', '*' => 'number', '%' => 'year', ); my %results; my $data = '0010$ADAM SMITH0003*3330004%19770004$BOB 0001*40004%1967'; my @tokens = split(/\d{4}([\$\*\%])/, $data); shift @tokens; #there is a "dead" field at the beginning while (@tokens) { my ($type, $value) = splice(@tokens,0,2); my $type_text = $xlatetype{$type}; push @{$results{$type_text}}, $value } print Dumper \%results; __END__ $VAR1 = { 'number' => [ '333', '4' ], 'string' => [ 'ADAM SMITH', 'BOB ' ], 'year' => [ '1977', '1967' ] };
In reply to Re: Splitting file into separate files based on record lengths and identifiers
by Marshall
in thread Splitting file into separate files based on record lengths and identifiers
by monty77
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |