G'day davi54,
"I want to count the number of alphabets in each entry, length of each entry, etc."
That's confusing as you combine the counts for all entries in @count.
Other variables, outside the while loop, appear misplaced as they look like they're associated with individual entries:
I'd expect them to be inside the while loop.
The following code collects all the data that I believe you want.
You can combine values for all entries if necessary.
#!/usr/bin/env perl
use strict;
use warnings;
my %results;
{
local $/ = '';
while (my $record = <DATA>) {
$record =~ s/\A>(.+?)$//m;
my $entry = $1;
$record =~ s/\s//gm;
$results{$entry}{len} = length $record;
for (0 .. $results{$entry}{len} - 1) {
++$results{$entry}{count}{substr $record, $_, 1};
}
}
}
use Data::Dump;
dd \%results;
__DATA__
>sp_0005
VQLQESGGGLVQAGGSLRLSCAASGRAVSMYNMGWFRQAPGQERELVAAISRGGSIYYA
DSVKGRFTISRDNAKNTLYLQMNNLKPEDTGVYQCRQGSTLGQGTQVTVSS
>sp_0017
HVQLVESGGGSVQAGGSLRLTCAASGFTFSNYYMSWVRQAPGKGLEWVSSIYSVGSNGYY
ADSVKGRSTISRDNAKNTLYLQMNSLKPEDTAVYYCAAEPGGSWWDAYSYWGQGTQVTVS S
Extract of output:
{
sp_0005 => {
count => {
A => 9,
C => 2,
...,
W => 1,
Y => 5,
},
len => 110,
},
sp_0017 => {
count => {
A => 10,
C => 2,
...,
W => 5,
Y => 10,
},
len => 121,
},
}
Notes:
-
You posted sample data but did so using paragraph text.
Please use <code> tags for this sort of thing.
I've made a best guess at what the original looked like:
I was unsure about the embedded space at the end of the second entry
(cf. 'QVTVSS' in 1st entry and 'QVTVS S' in 2nd entry)
so left it as written.
-
I've truncated the names to avoid excessive text wrapping.
They're still unique but now just look like sp_NNNN.
-
When you modify special variables, such as $/ here,
you should localise the change in the smallest scope possible
(see my code for an example of this).
-
I've used substr instead of a regex to get the counts.
Perl's string-handling functions are typically faster than regexes that achieve the same outcome.
-
Do note that this code is only intended to show a technique.
It does not contain any I/O or formatted reporting.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.