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

In reply to Re: concatenating strings by Laurent_R
in thread concatenating strings by alferic

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.