Here is my stab at making it dynamic.

#!/user/bin/perl use strict; use warnings; #----------- define field mappings --------------------- # hash to map from field i in input to field j in output # field i is the key to hash, field j is the value of hash my %from_to = ( 1 => 4, 2 => 2, 3 => 1, 4 => 3, ); # field length hash...key is the field number, value of hash # is the length in characters. Presumes the length of the # input and output fields are the same. my %field_len = ( 1 => 10, 2 => 4, 3 => 6, 4 => 6, ); #---------- setup decode string -------- my $decode_string = ""; foreach my $num (sort keys %from_to) {$decode_string .= 'A' . $field_len{$num} . ' '}; #-------------- process files ---------- my @input; my @output; foreach my $in_record (<DATA>) { chomp($in_record); print $in_record . " ---> "; @input =(unpack $decode_string,$in_record); foreach my $index (sort keys %from_to) {$output[($from_to{$index}-1)] = $input[($index-1)]}; my $out_record = join "",@output; print $out_record . "\n"; } exit(0); __END__ AAAAAAAAAA1111BBBBBB222222 BBBBBBBBBB2222CCCCCC333333 CCCCCCCCCC3333DDDDDD444444

I used the two hashes to contain the mapping of the input field to the output field (%from_to) and the field lengths (%field_len). You, of course, could use whatever strategy you want.

I also have prints in to see how things work.

I had tried to make it more compact by trying to use a dynamic strategy for specifying the array slice as part of the join..decode line. But I couln't figure out (or remember) how to do that.

ack Albuquerque, NM

In reply to Re^3: Seeking an Enlightened Path (Parsing, Translating, Translocating) by ack
in thread Seeking an Enlightened Path (Parsing, Translating, Translocating) by nanotasher

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.