alferic has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Guru's, I have this lines on my file"
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.00 0 0.00 | T6 BVEB 11 100.00 0 + 0.00 | T7 VFBE 11 100.00 0 0.00 | T8 VFBE 11 100.00 0 + 0.00 |
I wanted to split "|" and joined it into one that will look below
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.00 0 0.00 T4 ICBO 11 100.00 0 0.00 T6 BVEB 11 100.00 0 0.00 T8 VFBE 11 100.00 0 0.00
please help! Thanks!

Replies are listed 'Best First'.
Re: concatenating strings
by Laurent_R (Canon) on Sep 10, 2013 at 08:50 UTC

    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
Re: concatenating strings
by marinersk (Priest) on Sep 10, 2013 at 07:31 UTC
    Hello alferic,

    Please have mercy on us old folks; please use <p> and <br> tags to separate your paragraphs and lines; and for the data, <code> and </code> are very helpful.

    So you want to split and join the data. Suggest you post the Perl code you've tried thus far and we can help you adjust it into working the way you wish.

Re: concatenating strings
by hdb (Monsignor) on Sep 10, 2013 at 07:57 UTC

    I am guessing: you want the bits with T<odd number> first, then T<even number>? Here is one way to do it:

    use strict; use warnings; $_ = "T1 CONT 11 100.00 0 0.00 | T2 IEB 11 100.00 0 0.00 | T3 IEB 11 1 +00.00 0 0.00 | ". "T4 ICBO 11 100.00 0 0.00 | T5 ICBO 11 100.00 0 0.00 | T6 BVEB + 11 100.00 0 0.00 | ". "T7 VFBE 11 100.00 0 0.00 | T8 VFBE 11 100.00 0 0.00"; # dirty grep with side effects... print grep { (/^\s*T(\d+)/)[0] % 2 ? !print : 1 } split /[|]/;
Re: concatenating strings
by marinersk (Priest) on Sep 13, 2013 at 08:45 UTC
    Thank you for reformatting; what you are requesting makes more sense now.

    I have questions about the final sequence but I will let those go in favor of addressing the Perl elements first.

    Something like this should help get you started, but it does not produce output in the same order you requested:

    #!/usr/bin/perl -w use strict; # Loop through input filenames listed on command line foreach my $inpfnm (@ARGV) { # Try to open the file for read (input) if (!open INPFIL, "<$inpfnm") { print "ERROR: Cannot open input file '$inpfnm'\n"; } else { # Derive the output filename my $outfnm = $inpfnm . '-out.dat'; # Try to open the file for write (output) if (!open OUTFIL, ">$outfnm") { print "ERROR: Cannot open output file '$outfnm'\n"; } else { # Loop through lines from the input file while (my $inpbuf = <INPFIL>) { # Remove the newline at the end of the line chomp $inpbuf; # Split input by vertical bar (pipe) character my @inpelt = split /\|/, $inpbuf; # Go through each element from the split foreach my $inpelt (@inpelt) { if ($inpelt !~ /^\s*$/) { # Line is not blank. Proceed. # Prepare output buffer. my $outbuf = $inpelt; # Trim leading and trailing whitespace $outbuf =~ s/^\s+//; $outbuf =~ s/\s+$//; # Write output print OUTFIL "$outbuf\n"; } } } # Cleanup close OUTFIL; } # Cleanup close INPFIL; } } exit; __END__

    It gets a little more complicated to process them in the order you requested, but not too much.

    Let us know if you need the order you specified or if this sequence is sufficient to your needs.

Re: concatenating strings
by Anonymous Monk on Sep 10, 2013 at 14:00 UTC
    If you want for us to do something or to say something, please make it clear what exactly it is that you want. Right now all we can say is, "gee, that data looks pretty .. so what?" :-/