Update: I thought I was just adding an additional note to the choir here, but.......evidently not as new requirements surfaced below.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.