in reply to Perl noob struggling to loop through an array

$ echo ">Sequence_Header_A DNAsequence >Sequence_Header_B DNAsequence >Sequence_Header_C DNAsequence" | perl -e' while ( <> ) { next unless /^>/; ( my $format = $_ ) =~ s/\n/_%d\n%s\n/; chomp( my @data = split /(?=e)/, <> ); my $count; printf $format, ++$count, $_ for @data; } ' >Sequence_Header_A_1 DNAs >Sequence_Header_A_2 equ >Sequence_Header_A_3 enc >Sequence_Header_A_4 e >Sequence_Header_B_1 DNAs >Sequence_Header_B_2 equ >Sequence_Header_B_3 enc >Sequence_Header_B_4 e >Sequence_Header_C_1 DNAs >Sequence_Header_C_2 equ >Sequence_Header_C_3 enc >Sequence_Header_C_4 e

Replies are listed 'Best First'.
Re^2: Perl noob struggling to loop through an array
by microkorg (Initiate) on Oct 26, 2012 at 12:42 UTC
    Thanks for such quick responses aitap and jwkrahn,
    I am happy with the way my code cuts up the data but I am having a more fundamental problem with getting the loop to perform as I wish.

    Could you suggest a way of getting the 2 sections of my own code into a single loop (I realise that jwkrahn suggested a slightly different method, and this is very much appreciated) but I would quite like to get my own technique running smoothly as well.

    Thanks again for all your help.

Re^2: Perl noob struggling to loop through an array
by Anonymous Monk on Oct 26, 2012 at 17:01 UTC
    Hi jwkrahn, would you mind putting some comments into your code explaining each section? I am struggling to follow it through. Thanks, microkorg