in reply to Re^3: Seeking an Enlightened Path (Parsing, Translating, Translocating)
in thread Seeking an Enlightened Path (Parsing, Translating, Translocating)
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.
|
|---|