#!/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 (from # 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 indices # are the field number and the array values are the number of characters # 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 () { chomp($record); $record = join "",(unpack $decode_str,$record)[@from_to]; print $record . "\n"; } exit(0); __END__ AAAAAAAAAA1111BBBBBB222222 BBBBBBBBBB2222CCCCCC333333 CCCCCCCCCC3333DDDDDD444444 #### AAAAAAAAAA1111BBBBBB222222 BBBBBBBBBB2222CCCCCC333333 CCCCCCCCCC3333DDDDDD444444 #### 2222221111AAAAAAAAAABBBBBB 3333332222BBBBBBBBBBCCCCCC 4444443333CCCCCCCCCCDDDDDD