I figured out the compact way to make the single line join-unpack work while making it dynamic per nonotasher's request!

The revised version of my original codes is in the following ReadMore:

#!/usr/bin/perl use strict; use warnings; #------------ Declare hashes -------------------------- # # Array to contain the mapping from the input record to the output # record. The array index are the input record's field numbers, i (fr +om # to n-1 where n are the number of fields; # the array value at the corresponding index is the output records, # field number, j. # my @from_to = (4,2,1,3); # convert index values into values from 0 to n-1 my $len_array = scalar @from_to; for(my $i = 0; $i<$len_array; $i++){$from_to[$i]--}; # # Array to determine number of characters in each field. The array i +ndices # are the field number and the array values are the number of charact +ers # in that field. As with @from_to, this array's indices go from 0 to +n-1 # where n is the number of fields. # my @field_len = (10,4,6,6); #------ Construct the decode string -------------------------- my $decode_str; foreach my $len (@field_len){$decode_str .= 'A'.$len.' '}; #--------- Process each Input Record ------------------------- foreach my $record (<DATA>) { chomp($record); $record = join "",(unpack $decode_str,$record)[@from_to]; print $record . "\n"; } exit(0); __END__ AAAAAAAAAA1111BBBBBB222222 BBBBBBBBBB2222CCCCCC333333 CCCCCCCCCC3333DDDDDD444444

Which yields nanotasher's original desired result for:

Input

AAAAAAAAAA1111BBBBBB222222 BBBBBBBBBB2222CCCCCC333333 CCCCCCCCCC3333DDDDDD444444

Output

2222221111AAAAAAAAAABBBBBB 3333332222BBBBBBBBBBCCCCCC 4444443333CCCCCCCCCCDDDDDD

This utilizes the wonderful single line join-unpack solution of BrowserUk but allows it to be dynamic.

ack Albuquerque, NM

In reply to Re^4: 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.