http://www.perlmonks.org?node_id=11113588


in reply to Assigning multiple lines into first element of array

Read to the next "\n>" and then fix up the line ends.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11113581 use warnings; my $filename = 'file.fasta'; open my $handle, '<', $filename or die "$! opening $filename"; my @array = map s/>\z//r =~ s/^(?!>)/>/r, do { local $/ ="\n>"; <$hand +le> }; use Data::Dumper; print Dumper \@array;

Or read the whole thing and split with a look-ahead for >

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11113581 use warnings; my $filename = 'file.fasta'; open my $handle, '<', $filename or die "$! opening $filename"; my @array = split /(?=>)/, do { local $/; <$handle> }; use Data::Dumper; print Dumper \@array;

Replies are listed 'Best First'.
Re^2: Assigning multiple lines into first element of array
by shabird (Sexton) on Mar 01, 2020 at 02:13 UTC

    Thank you so much! it worked like a charm :)

Re^2: Assigning multiple lines into first element of array
by shabird (Sexton) on Mar 01, 2020 at 03:09 UTC

    Everything works good but one more thing how can i count number of characters in first element of the array? When it returns first element of array it has many characters e.g: GAGGTGCTGGGGAGCAGCGTGTTTGCTGTGCTTGATTGTGAGCTGCTGGGAAGTTGTGACTTTCATTTTA CCTTTCGAATTCCTGGGTATATCTTGGGGGCTGGAGGACGTGTCTGGTTATTATATAGGTGCACAGCTGG. how come i count these characters?

      See length:

      c:\@Work\Perl\monks>perl -wMstrict -le "my @ra = ('MANYCHARACTERS'); printf qq{1st element of array has %d characters \n}, length $ra[0]; " 1st element of array has 14 characters


      Give a man a fish:  <%-{-{-{-<

      ...

      my @counts = map s/.*\n//r =~ tr/A-Z//, @array; print "counts: @counts\n";