Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Assigning multiple lines into first element of array

by tybalt89 (Prior)
on Feb 29, 2020 at 22:42 UTC ( #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? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2021-08-02 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (28 votes). Check out past polls.

    Notices?