in reply to Help in joining these lines

This approach follows your statement of the problem.
use strict; use warnings; use List::MoreUtils qw(first_index); my $sample = \do{my $raw = "> gi|11SB_CUCMA Train|1 21\n" ."MARSSLFTFLCLAVFINGCLSQIEQQSPWEFQGS\n" ."EVWQQHRYQSPRACRLENLRAQDPVRLLLPGFSNAPKLIFV\n" ."AQGFGIRGIAIPGCAETYQT\n" ."SSSSSSSSSSSSSSSSSSSSS....................\n" ."...........................\n" .".................\n" ."..........\n" ."> gi|1A43_HUMAN Train|1 24\n" ."MAVMAPRTLVLLLSGALALTQTWAGSHSMRYFYTSVSRPG\n" ."RGEPRFIAVGYVDDTQFVRFDSDAASQRMEPRAPWIEQEG" ."PEYWSQTDRANLGTLRGYYNQSEDGSHTIQR\n" ."MYGCDVGPDGRFLRGYQQDAYDGKDYIALNEDLRS" ."WTAADMAAQITQRKWETAHEAE\n" ."SSSSSSSSSSSSSSSSSSSSSSSS..........." ."...................................\n" ."...........................................\n" .".............\n" ."..........................................\n" }; open my $SAMPLE, '<', $sample; while (my $block = do{local $/ = "\n>"; <$SAMPLE>}) { $block =~ s/\n>\z//; $block =~ s/\A([^>])(.*?)$/>$1$2/ms; my @lines = split /\n/, $block; my $i = first_index {$_ =~ /\./} @lines; $" = q(); print $lines[0], "\n" ."@lines[1..$i-1]\n" ."@lines[$i..$#lines]\n"; }
Bill

Replies are listed 'Best First'.
Re^2: Help in joining these lines
by GrandFather (Saint) on Sep 16, 2016 at 02:45 UTC

    Instead of going through hoops with do and various variables you could just:

    open my $fIn, '<', \<<INSTR; > gi|11SB_CUCMA Train|1 21 ... .......................................... INSTR

    which saves a bunch of quoting things and messing around generally.

    Premature optimization is the root of all job security

      You do have a point. Of course it would be even easier to use <DATA>. I felt that the quoting offered three advantages.

      The biggest advantage is that it removes the ambiguity related to white space at the end of each line.

      All the data remains on the screen at once.

      The format of the open statement is exactly the same as for reading a disk file.

      Bill

        No, I think your use of a string is the right thing to do in this example. Having the open near the same as you would use for a file is good, especially to demonstrate the three parameter open. I'd tend to use a normal string rather than a string ref just to make it clear magic is happening though.

        I probably went one step too far putting the HEREDOC in the open though, but I was so chuffed to see it worked like that that I couldn't help it. :-D

        I don't think there was ever an issue of trailing white space in the OP's question and I don't see how the "data remains on the screen" comment applies. However, minor points and nothing to really worry about. More to do with style differences than right or wrong ways to do things. In Perl I almost never use the concatenation operator because Perl offers so many other ways to get the job done that appeal more to my eye, and for multiple line stuff my go to tool is the HEREDOC because in code it most looks as it's going to look as output.

        Premature optimization is the root of all job security