Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Assigning multiple lines into first element of array

by tybalt89 (Monsignor)
on Feb 29, 2020 at 22:42 UTC ( [id://11113588]=note: print w/replies, xml ) Need Help??


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";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11113588]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-29 15:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found