in reply to concatenating strings
You gave a perfectly valid and easy solution in your question: split and join. Possibly something like this, illustrated under the Perl debugger:
DB<1> $line = "T1 CONT 11 100.00 0 0.00 | T2 IEB 11 100.00 0 0.00 | +T3 IEB 11 100.00 0 0.00 | T4 ICBO 11 100.00 0 0.00 | T5 ICBO 11 100.0 +0 0 0.00 | T6 BVEB 11 100.00 0 0.00 | T7 VFBE 11 100.00 0 0.00 | T8 V +FBE 11 100.00 0 0.00 |"; DB<2> @fields = split /\|/, $line; DB<3> x @fields 0 'T1 CONT 11 100.00 0 0.00 ' 1 ' T2 IEB 11 100.00 0 0.00 ' 2 ' T3 IEB 11 100.00 0 0.00 ' 3 ' T4 ICBO 11 100.00 0 0.00 ' 4 ' T5 ICBO 11 100.00 0 0.00 ' 5 ' T6 BVEB 11 100.00 0 0.00 ' 6 ' T7 VFBE 11 100.00 0 0.00 ' 7 ' T8 VFBE 11 100.00 0 0.00 ' DB<4> $new_string = join " ", @fields[0,2,4,6,1,3,5,7]; DB<5> print $new_string; T1 CONT 11 100.00 0 0.00 T3 IEB 11 100.00 0 0.00 T5 ICBO 11 100.00 + 0 0.00 T7 VFBE 11 100.00 0 0.00 T2 IEB 11 100.00 0 0.00 T4 ICB +O 11 100.00 0 0.00 T6 BVEB 11 100.00 0 0.00 T8 VFBE 11 100.00 0 0 +.00
If you don't want the extra-spaces added between the temporary array elements, you can change it to something like this:
DB<8> @fields = split /\| ?/, $line; DB<9> $new_string = join "", @fields[0,2,4,6,1,3,5,7]; DB<10> print $new_string; T1 CONT 11 100.00 0 0.00 T3 IEB 11 100.00 0 0.00 T5 ICBO 11 100.00 0 0 +.00 T7 VFBE 11 100.00 0 0.00 T2 IEB 11 100.00 0 0.00 T4 ICBO 11 100.0 +0 0 0.00 T6 BVEB 11 100.00 0 0.00 T8 VFBE 11 100.00 0 0.00
|
|---|